forked from WXLmjr/VirtualOscilloscope_Qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplingthread.cpp
79 lines (66 loc) · 2.13 KB
/
samplingthread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "samplingthread.h"
#include "signaldata.h"
#include <qwt_math.h>
#include <math.h>
#if QT_VERSION < 0x040600
#define qFastSin(x) ::sin(x)
#endif
SamplingThread::SamplingThread( QObject *parent ):
QwtSamplingThread( parent ),
d_frequency( 5.0 ),
d_amplitude( 20.0 )
{
}
void SamplingThread::setFrequency( double frequency )
{
d_frequency = frequency;
}
double SamplingThread::frequency() const
{
return d_frequency;
}
void SamplingThread::setAmplitude( double amplitude )
{
d_amplitude = amplitude;
}
double SamplingThread::amplitude() const
{
return d_amplitude;
}
//elapsed代表时间的流逝,x轴即时间轴。
void SamplingThread::sample( double elapsed )
{
if ( d_frequency > 0.0 )
{
//通过x的值,计算y轴的值。
const QPointF s( elapsed, value( elapsed ) );
//将产生的采样点添加到静态局部类SignalData对象valueVector
SignalData::instance().append( s );
}
}
//y轴与x轴的函数关系,此处为正弦函数y = Asin(wt)
double SamplingThread::value( double timeStamp ) const
{
const double period = 1.0 / d_frequency;
/*
* 产生正弦波形的代码
* fmod() 用来对浮点数进行取模(求余)
*/
const double x = ::fmod( timeStamp, period );
const double v = d_amplitude * qFastSin( x / period * 2 * M_PI );
return v;
}
/* 产生正切函数波形的代码 */
// const double x = ::fmod( timeStamp, period ); //fmod() 用来对浮点数进行取模(求余)
// const double v = (qFastSin( x / period * 2 * M_PI )) / (qFastCos( x / period * 2 * M_PI ));
/* 产生三角波的代码 */
// const double x = ::fmod( timeStamp, period ); //fmod() 用来对浮点数进行取模(求余)
// double v = 0;
// if(x<(period/2.0)) {
// v = d_amplitude * (2.0*x/period) - d_amplitude/2.0;
// } else {
// v = d_amplitude * (-2.0*x/period + 2) - d_amplitude/2.0;
// }
/* 产生锯齿波的代码 */
// const double x = ::fmod( timeStamp, period ); //fmod() 用来对浮点数进行取模(求余)
// const double v = d_amplitude * x/period - d_amplitude/2.0;