aboutsummaryrefslogtreecommitdiffstats
path: root/gr-qtgui/lib/SpectrumGUIClass.cc
blob: 4cd2e1cb341ecc14d9d67c0e92322e88fd2bba20 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* -*- c++ -*- */
/*
 * Copyright 2008-2011 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifndef SPECTRUM_GUI_CLASS_CPP
#define SPECTRUM_GUI_CLASS_CPP

#include <gnuradio/qtgui/SpectrumGUIClass.h>

// Added by qt3to4:
#include <volk/volk.h>
#include <QEvent>

const long SpectrumGUIClass::MAX_FFT_SIZE = 32768;
const long SpectrumGUIClass::MIN_FFT_SIZE = 32;
const long SpectrumGUIClass::DEFAULT_FFT_SIZE = 1024;

SpectrumGUIClass::SpectrumGUIClass(const uint64_t maxDataSize,
                                   const uint64_t fftSize,
                                   const double newCenterFrequency,
                                   const double newStartFrequency,
                                   const double newStopFrequency)
    : _dataPoints(std::max(static_cast<int64_t>(maxDataSize), static_cast<int64_t>(2))),
      _centerFrequency(newCenterFrequency),
      _startFrequency(newStartFrequency),
      _stopFrequency(newStopFrequency),
      _lastDataPointCount(_dataPoints),
      _fftSize(fftSize)
{
}

SpectrumGUIClass::~SpectrumGUIClass()
{
    // We don't need to delete this since as a QWidget, it is supposed to be destroyed
    // with it's parent. Deleting it causes a segmentation fault, and not deleting it
    // does not leave any extra memory.
    // if(getWindowOpenFlag()){
    // delete _spectrumDisplayForm;
    //}
}

void SpectrumGUIClass::openSpectrumWindow(QWidget* parent,
                                          const bool frequency,
                                          const bool waterfall,
                                          const bool time,
                                          const bool constellation)
{
    d_mutex.lock();

    if (!_windowOpennedFlag) {

        if (_fftPoints.empty()) {
            _fftPoints.resize(_dataPoints);
            _realTimeDomainPoints.resize(_dataPoints);
            _imagTimeDomainPoints.resize(_dataPoints);
        }

        // Called from the Event Thread
        _spectrumDisplayForm = new SpectrumDisplayForm(parent);

        // Toggle Windows on/off
        _spectrumDisplayForm->toggleTabFrequency(frequency);
        _spectrumDisplayForm->toggleTabWaterfall(waterfall);
        _spectrumDisplayForm->toggleTabTime(time);
        _spectrumDisplayForm->toggleTabConstellation(constellation);

        _windowOpennedFlag = true;

        _spectrumDisplayForm->setSystem(this, _dataPoints, _fftSize);

        qApp->processEvents();
    }
    d_mutex.unlock();


    setDisplayTitle(_title);
    reset();

    qApp->postEvent(_spectrumDisplayForm, new QEvent(QEvent::Type(QEvent::User + 3)));

    _lastGUIUpdateTime = 0;

    // Draw Blank Display
    updateWindow(false, NULL, 0, NULL, 0, NULL, 0, gr::high_res_timer_now(), true);

    // Set up the initial frequency axis settings
    setFrequencyRange(_centerFrequency, _startFrequency, _stopFrequency);

    // GUI Thread only
    qApp->processEvents();

    // Set the FFT Size combo box to display the right number
    int idx =
        _spectrumDisplayForm->FFTSizeComboBox->findText(QString("%1").arg(_fftSize));
    _spectrumDisplayForm->FFTSizeComboBox->setCurrentIndex(idx);
}

void SpectrumGUIClass::reset()
{
    if (getWindowOpenFlag()) {
        qApp->postEvent(_spectrumDisplayForm,
                        new SpectrumFrequencyRangeEvent(
                            _centerFrequency, _startFrequency, _stopFrequency));
        qApp->postEvent(_spectrumDisplayForm, new SpectrumWindowResetEvent());
    }
    _droppedEntriesCount = 0;
    // Call the following function from the Spectrum Window Reset Event window
    // ResetPendingGUIUpdateEvents();
}

void SpectrumGUIClass::setDisplayTitle(const std::string newString)
{
    _title.assign(newString);

    if (getWindowOpenFlag()) {
        qApp->postEvent(_spectrumDisplayForm,
                        new SpectrumWindowCaptionEvent(_title.c_str()));
    }
}

bool SpectrumGUIClass::getWindowOpenFlag()
{
    gr::thread::scoped_lock lock(d_mutex);
    return _windowOpennedFlag;
}


void SpectrumGUIClass::setWindowOpenFlag(const bool newFlag)
{
    gr::thread::scoped_lock lock(d_mutex);
    _windowOpennedFlag = newFlag;
}

void SpectrumGUIClass::setFrequencyRange(const double centerFreq,
                                         const double startFreq,
                                         const double stopFreq)
{
    gr::thread::scoped_lock lock(d_mutex);
    _centerFrequency = centerFreq;
    _startFrequency = startFreq;
    _stopFrequency = stopFreq;

    qApp->postEvent(_spectrumDisplayForm,
                    new SpectrumFrequencyRangeEvent(
                        _centerFrequency, _startFrequency, _stopFrequency));
}

double SpectrumGUIClass::getStartFrequency()
{
    gr::thread::scoped_lock lock(d_mutex);
    double returnValue = 0.0;
    returnValue = _startFrequency;
    return returnValue;
}

double SpectrumGUIClass::getStopFrequency()
{
    gr::thread::scoped_lock lock(d_mutex);
    double returnValue = 0.0;
    returnValue = _stopFrequency;
    return returnValue;
}

double SpectrumGUIClass::getCenterFrequency()
{
    gr::thread::scoped_lock lock(d_mutex);
    double returnValue = 0.0;
    returnValue = _centerFrequency;
    return returnValue;
}


void SpectrumGUIClass::updateWindow(const bool updateDisplayFlag,
                                    const float* fftBuffer,
                                    const uint64_t inputBufferSize,
                                    const float* realTimeDomainData,
                                    const uint64_t realTimeDomainDataSize,
                                    const float* complexTimeDomainData,
                                    const uint64_t complexTimeDomainDataSize,
                                    const gr::high_res_timer_type timestamp,
                                    const bool lastOfMultipleFFTUpdateFlag)
{
    // gr::thread::scoped_lock lock(d_mutex);
    int64_t bufferSize = inputBufferSize;
    bool repeatDataFlag = false;
    if (bufferSize > _dataPoints) {
        bufferSize = _dataPoints;
    }
    int64_t timeDomainBufferSize = 0;

    if (updateDisplayFlag) {
        if ((fftBuffer != NULL) && (bufferSize > 0)) {
            std::copy(fftBuffer, fftBuffer + bufferSize, std::begin(_fftPoints));
        }

        // ALL OF THIS SHIT SHOULD BE COMBINED WITH THE FFTSHIFT
        // USE VOLK_32FC_DEINTERLEAVE_64F_X2_A TO GET REAL/IMAG FROM COMPLEX32
        // Can't do a memcpy since this is going from float to double data type
        if ((realTimeDomainData != NULL) && (realTimeDomainDataSize > 0)) {
            const float* realTimeDomainDataPtr = realTimeDomainData;

            double* realTimeDomainPointsPtr = _realTimeDomainPoints.data();
            timeDomainBufferSize = realTimeDomainDataSize;

            std::fill(
                std::begin(_imagTimeDomainPoints), std::end(_imagTimeDomainPoints), 0.0);
            for (uint64_t number = 0; number < realTimeDomainDataSize; number++) {
                *realTimeDomainPointsPtr++ = *realTimeDomainDataPtr++;
            }
        }

        if ((complexTimeDomainData != NULL) && (complexTimeDomainDataSize > 0)) {
            volk_32fc_deinterleave_64f_x2(_realTimeDomainPoints.data(),
                                          _imagTimeDomainPoints.data(),
                                          (const lv_32fc_t*)complexTimeDomainData,
                                          complexTimeDomainDataSize);
            timeDomainBufferSize = complexTimeDomainDataSize;
        }
    }

    // If bufferSize is zero, then just update the display by sending over the old data
    if (bufferSize < 1) {
        bufferSize = _lastDataPointCount;
        repeatDataFlag = true;
    } else {
        // Since there is data this time, update the count
        _lastDataPointCount = bufferSize;
    }

    const gr::high_res_timer_type currentTime = gr::high_res_timer_now();
    const gr::high_res_timer_type lastUpdateGUITime = getLastGUIUpdateTime();

    if ((currentTime - lastUpdateGUITime >
         (4 * _updateTime) * gr::high_res_timer_tps()) &&
        (getPendingGUIUpdateEvents() > 0) && lastUpdateGUITime != 0) {
        // Do not update the display if too much data is pending to be displayed
        _droppedEntriesCount++;
    } else {
        // Draw the Data
        incrementPendingGUIUpdateEvents();
        qApp->postEvent(_spectrumDisplayForm,
                        new SpectrumUpdateEvent(_fftPoints.data(),
                                                bufferSize,
                                                _realTimeDomainPoints.data(),
                                                _imagTimeDomainPoints.data(),
                                                timeDomainBufferSize,
                                                timestamp,
                                                repeatDataFlag,
                                                lastOfMultipleFFTUpdateFlag,
                                                currentTime,
                                                _droppedEntriesCount));

        // Only reset the dropped entries counter if this is not
        // repeat data since repeat data is dropped by the display systems
        if (!repeatDataFlag) {
            _droppedEntriesCount = 0;
        }
    }
}

float SpectrumGUIClass::getPowerValue()
{
    gr::thread::scoped_lock lock(d_mutex);
    float returnValue = 0;
    returnValue = _powerValue;
    return returnValue;
}

void SpectrumGUIClass::setPowerValue(const float value)
{
    gr::thread::scoped_lock lock(d_mutex);
    _powerValue = value;
}

int SpectrumGUIClass::getWindowType()
{
    gr::thread::scoped_lock lock(d_mutex);
    int returnValue = 0;
    returnValue = _windowType;
    return returnValue;
}

void SpectrumGUIClass::setWindowType(const int newType)
{
    gr::thread::scoped_lock lock(d_mutex);
    _windowType = newType;
}

int SpectrumGUIClass::getFFTSize()
{
    int returnValue = 0;
    returnValue = _fftSize;
    return returnValue;
}

int SpectrumGUIClass::getFFTSizeIndex()
{
    gr::thread::scoped_lock lock(d_mutex);
    int fftsize = getFFTSize();
    int rv = 0;
    switch (fftsize) {
    case (1024):
        rv = 0;
        break;
    case (2048):
        rv = 1;
        break;
    case (4096):
        rv = 2;
        break;
    case (8192):
        rv = 3;
        break;
    case (16384):
        rv = 3;
        break;
    case (32768):
        rv = 3;
        break;
    default:
        rv = 0;
        break;
    }
    return rv;
}

void SpectrumGUIClass::setFFTSize(const int newSize)
{
    gr::thread::scoped_lock lock(d_mutex);
    _fftSize = newSize;
}

gr::high_res_timer_type SpectrumGUIClass::getLastGUIUpdateTime()
{
    gr::thread::scoped_lock lock(d_mutex);
    gr::high_res_timer_type returnValue;
    returnValue = _lastGUIUpdateTime;
    return returnValue;
}

void SpectrumGUIClass::setLastGUIUpdateTime(const gr::high_res_timer_type newTime)
{
    gr::thread::scoped_lock lock(d_mutex);
    _lastGUIUpdateTime = newTime;
}

unsigned int SpectrumGUIClass::getPendingGUIUpdateEvents()
{
    gr::thread::scoped_lock lock(d_mutex);
    unsigned int returnValue = 0;
    returnValue = _pendingGUIUpdateEventsCount;
    return returnValue;
}

void SpectrumGUIClass::incrementPendingGUIUpdateEvents()
{
    gr::thread::scoped_lock lock(d_mutex);
    _pendingGUIUpdateEventsCount++;
}

void SpectrumGUIClass::decrementPendingGUIUpdateEvents()
{
    gr::thread::scoped_lock lock(d_mutex);
    if (_pendingGUIUpdateEventsCount > 0) {
        _pendingGUIUpdateEventsCount--;
    }
}

void SpectrumGUIClass::resetPendingGUIUpdateEvents()
{
    gr::thread::scoped_lock lock(d_mutex);
    _pendingGUIUpdateEventsCount = 0;
}


QWidget* SpectrumGUIClass::qwidget()
{
    gr::thread::scoped_lock lock(d_mutex);
    return (QWidget*)_spectrumDisplayForm;
}

void SpectrumGUIClass::setTimeDomainAxis(double min, double max)
{
    gr::thread::scoped_lock lock(d_mutex);
    _spectrumDisplayForm->setTimeDomainAxis(min, max);
}

void SpectrumGUIClass::setConstellationAxis(double xmin,
                                            double xmax,
                                            double ymin,
                                            double ymax)
{
    gr::thread::scoped_lock lock(d_mutex);
    _spectrumDisplayForm->setConstellationAxis(xmin, xmax, ymin, ymax);
}

void SpectrumGUIClass::setConstellationPenSize(int size)
{
    gr::thread::scoped_lock lock(d_mutex);
    _spectrumDisplayForm->setConstellationPenSize(size);
}


void SpectrumGUIClass::setFrequencyAxis(double min, double max)
{
    gr::thread::scoped_lock lock(d_mutex);
    _spectrumDisplayForm->setFrequencyAxis(min, max);
}

void SpectrumGUIClass::setUpdateTime(double t)
{
    gr::thread::scoped_lock lock(d_mutex);
    _updateTime = t;
    _spectrumDisplayForm->setUpdateTime(_updateTime);
}

void SpectrumGUIClass::enableRFFreq(bool en)
{
    gr::thread::scoped_lock lock(d_mutex);
    _spectrumDisplayForm->toggleRFFrequencies(en);
}

bool SpectrumGUIClass::checkClicked()
{
    gr::thread::scoped_lock lock(d_mutex);
    return _spectrumDisplayForm->checkClicked();
}

float SpectrumGUIClass::getClickedFreq()
{
    gr::thread::scoped_lock lock(d_mutex);
    return _spectrumDisplayForm->getClickedFreq();
}

#endif /* SPECTRUM_GUI_CLASS_CPP */