Removing Waterfall3DPlot. The qwt_plot3d is too much of a hassle to deal with and...
[debian/gnuradio] / gr-qtgui / src / lib / waterfallGlobalData.cc
1 #ifndef WATERFALL_GLOBAL_DATA_CPP
2 #define WATERFALL_GLOBAL_DATA_CPP
3
4 #include <waterfallGlobalData.h>
5
6 WaterfallData::WaterfallData(const double minimumFrequency,
7                              const double maximumFrequency,
8                              const uint64_t fftPoints,
9                              const unsigned int historyExtent)
10   : QwtRasterData(QwtDoubleRect(minimumFrequency /* X START */,  0 /* Y START */, 
11                                 maximumFrequency - minimumFrequency /* WIDTH */,  
12                                 static_cast<double>(historyExtent)/* HEIGHT */))
13 {
14   _intensityRange = QwtDoubleInterval(-200.0, 0.0);
15   
16   _fftPoints = fftPoints;
17   _historyLength = historyExtent;
18
19   _spectrumData = new double[_fftPoints * _historyLength];
20
21   Reset();
22 }
23
24 WaterfallData::~WaterfallData()
25 {
26   delete[] _spectrumData;
27 }
28
29 void WaterfallData::Reset()
30 {
31   memset(_spectrumData, 0x0, _fftPoints*_historyLength*sizeof(double));
32
33   _numLinesToUpdate = -1;
34 }
35
36 void WaterfallData::Copy(const WaterfallData* rhs)
37 {
38   if((_fftPoints != rhs->GetNumFFTPoints()) ||
39      (boundingRect() != rhs->boundingRect()) ){
40     _fftPoints = rhs->GetNumFFTPoints();
41     setBoundingRect(rhs->boundingRect());
42     delete[] _spectrumData;
43     _spectrumData = new double[_fftPoints * _historyLength];
44   }
45   Reset();
46   SetSpectrumDataBuffer(rhs->GetSpectrumDataBuffer());
47   SetNumLinesToUpdate(rhs->GetNumLinesToUpdate());
48   setRange(rhs->range());
49 }
50
51 void WaterfallData::ResizeData(const double startFreq,
52                                const double stopFreq,
53                                const uint64_t fftPoints)
54 {
55   if((fftPoints != GetNumFFTPoints()) ||
56      (boundingRect().width() != (stopFreq - startFreq)) ||
57      (boundingRect().left() != startFreq)){
58
59     setBoundingRect(QwtDoubleRect(startFreq, 0, stopFreq-startFreq, boundingRect().height()));
60     _fftPoints = fftPoints;
61     delete[] _spectrumData;
62     _spectrumData = new double[_fftPoints * _historyLength];
63   }
64    
65   Reset();
66 }
67
68 QwtRasterData *WaterfallData::copy() const
69 {
70   WaterfallData* returnData =  new WaterfallData(boundingRect().left(),
71                                                  boundingRect().right(),
72                                                  _fftPoints, _historyLength);
73   returnData->Copy(this);
74   return returnData;
75 }
76
77 QwtDoubleInterval WaterfallData::range() const
78 {
79   return _intensityRange;
80 }
81
82 void WaterfallData::setRange(const QwtDoubleInterval& newRange)
83 {
84   _intensityRange = newRange;
85 }
86
87 double WaterfallData::value(double x, double y) const
88 {
89   double returnValue = 0.0;
90
91   const unsigned int intY = static_cast<unsigned int>((1.0 - (y/boundingRect().height())) * 
92                                                       static_cast<double>(_historyLength - 1));
93   const unsigned int intX = static_cast<unsigned int>((((x - boundingRect().left()) / boundingRect().width()) * 
94                                                        static_cast<double>(_fftPoints-1)) + 0.5);
95
96   const int location = (intY * _fftPoints) + intX;
97   if((location > -1) && (location < static_cast<int64_t>(_fftPoints * _historyLength))){
98     returnValue = _spectrumData[location];
99   }
100
101   return returnValue;
102 }
103
104 uint64_t WaterfallData::GetNumFFTPoints() const
105 {
106   return _fftPoints;
107 }
108
109 void WaterfallData::addFFTData(const double* fftData,
110                                const uint64_t fftDataSize,
111                                const int droppedFrames){
112   if(fftDataSize == _fftPoints){
113     int64_t heightOffset = _historyLength - 1 - droppedFrames;
114     uint64_t drawingDroppedFrames = droppedFrames;
115
116     // Any valid data rolled off the display so just fill in zeros and write new data
117     if(heightOffset < 0){
118       heightOffset = 0;
119       drawingDroppedFrames = static_cast<uint64_t>(_historyLength-1);
120     }
121     
122     // Copy the old data over if any available
123     if(heightOffset > 0){
124       memmove( _spectrumData, &_spectrumData[(drawingDroppedFrames+1) * _fftPoints],
125                heightOffset * _fftPoints * sizeof(double)) ;
126     }
127
128     if(drawingDroppedFrames > 0){
129       // Fill in zeros data for dropped data
130       memset(&_spectrumData[heightOffset * _fftPoints], 0x00,
131              static_cast<int64_t>(drawingDroppedFrames) * _fftPoints * sizeof(double));
132     }
133
134     // add the new buffer
135     memcpy(&_spectrumData[(_historyLength - 1) * _fftPoints], fftData, _fftPoints*sizeof(double));
136   }
137 }
138
139 double* WaterfallData::GetSpectrumDataBuffer() const
140 {
141   return _spectrumData;
142 }
143
144 void WaterfallData::SetSpectrumDataBuffer(const double* newData)
145 {
146   memcpy(_spectrumData, newData, _fftPoints * _historyLength * sizeof(double));
147 }
148
149 int WaterfallData::GetNumLinesToUpdate() const
150 {
151   return _numLinesToUpdate;
152 }
153
154 void WaterfallData::SetNumLinesToUpdate(const int newNum)
155 {
156   _numLinesToUpdate = newNum;
157 }
158
159 void WaterfallData::IncrementNumLinesToUpdate()
160 {
161   _numLinesToUpdate++;
162 }
163
164 #endif /* WATERFALL_GLOBAL_DATA_CPP */