Reputation: 427
the example of qwt oscillocope generate a sinus waveform, for my project I would like to generate a Square or cosinus wave. I tried to find out where the sinus wave is generated , I did'nt find. any help will be appreciated thx
Upvotes: 1
Views: 3833
Reputation: 2313
To generate a 'Mathmatical square wave" meaning non band-limited square wave use the following code:
double value = sin(x / period * 2.0 * M_PI)>=0.0 ? 1.0:-1.0;
This will give you a wave that would theoretically be the pure analog square wave. As I mentioned above this wave will be non band limited in that it is not going to sound correct because of aliasing. But if you are just using it for a oscilloscope that does not have audio output it will look correct.
Let Me know if this helps.
EDIT: For saw...
const double Sample_Rate = 44100.0;
typedef struct Saw_Data{
double _phasor = 0.0;
double _tolerance = 1.0;
}Saw_Data;
double _DSP::Saw_Wave(double* _frequency,_DSP::Saw_Data* _data){
double _val = _data->_phasor;
_data->_phasor += 2.0 * (1.0/(Sample_Rate/ *_frequency));
if (_data->_phasor > _data->_tolerance) {
_data->_phasor-=2.0;
}
return _val ;
}
This is the code i use for a "Mathematical" non band limited saw wave. Lemme know if you need me to explain it.
Upvotes: 3
Reputation: 15501
Searching the oscilloscope directory for the string "sin" reveals that the sin wave is generated in samplingthread.cpp
. Here is the relevant method:
double SamplingThread::value( double timeStamp ) const
{
const double period = 1.0 / d_frequency;
const double x = ::fmod( timeStamp, period );
const double v = d_amplitude * qFastSin( x / period * 2 * M_PI );
return v;
}
Upvotes: 0