3 * Copyright 2008,2009 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <qtgui_sink_c.h>
28 #include <gr_io_signature.h>
34 qtgui_make_sink_c (int fftsize, int wintype,
35 float fmin, float fmax,
36 const std::string &name,
37 bool plotfreq, bool plotwaterfall,
38 bool plotwaterfall3d, bool plottime,
43 return qtgui_sink_c_sptr (new qtgui_sink_c (fftsize, wintype,
45 plotfreq, plotwaterfall,
46 plotwaterfall3d, plottime,
52 qtgui_sink_c::qtgui_sink_c (int fftsize, int wintype,
53 float fmin, float fmax,
54 const std::string &name,
55 bool plotfreq, bool plotwaterfall,
56 bool plotwaterfall3d, bool plottime,
61 gr_make_io_signature (1, -1, sizeof(gr_complex)),
62 gr_make_io_signature (0, 0, 0)),
64 d_wintype((gr_firdes::win_type)(wintype)),
65 d_fmin(fmin), d_fmax(fmax), d_name(name),
66 d_plotfreq(plotfreq), d_plotwaterfall(plotwaterfall),
67 d_plotwaterfall3d(plotwaterfall3d), d_plottime(plottime),
68 d_plotconst(plotconst),
72 pthread_mutex_init(&d_pmutex, NULL);
75 // Perform fftshift operation;
76 // this is usually desired when plotting
79 d_fft = new gri_fft_complex (d_fftsize, true);
81 d_fftdata = new gr_complex[d_fftsize];
84 d_residbuf = new gr_complex[d_fftsize];
88 initialize(use_openGL);
91 qtgui_sink_c::~qtgui_sink_c()
99 void qtgui_sink_c::lock()
101 pthread_mutex_lock(&d_pmutex);
104 void qtgui_sink_c::unlock()
106 pthread_mutex_unlock(&d_pmutex);
111 qtgui_sink_c::initialize(const bool opengl)
114 d_qApplication = qApp;
119 d_qApplication = new QApplication(argc, argv);
122 uint64_t maxBufferSize = 32768;
123 d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize,
126 d_main_gui->SetDisplayTitle(d_name);
127 d_main_gui->SetFFTSize(d_fftsize);
128 d_main_gui->SetWindowType((int)d_wintype);
130 d_main_gui->OpenSpectrumWindow(d_parent,
131 d_plotfreq, d_plotwaterfall,
132 d_plotwaterfall3d, d_plottime,
136 d_object = new qtgui_obj(d_qApplication);
137 qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
142 qtgui_sink_c::exec_()
144 d_qApplication->exec();
148 qtgui_sink_c::qwidget()
150 return d_main_gui->qwidget();
154 qtgui_sink_c::pyqwidget()
156 PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
157 PyObject *retarg = Py_BuildValue("N", w);
162 qtgui_sink_c::set_frequency_range(const double centerfreq,
163 const double startfreq,
164 const double stopfreq)
166 d_main_gui->SetFrequencyRange(centerfreq, startfreq, stopfreq);
170 qtgui_sink_c::set_time_domain_axis(double min, double max)
172 d_main_gui->SetTimeDomainAxis(min, max);
176 qtgui_sink_c::set_constellation_axis(double xmin, double xmax,
177 double ymin, double ymax)
179 d_main_gui->SetConstellationAxis(xmin, xmax, ymin, ymax);
183 qtgui_sink_c::set_frequency_axis(double min, double max)
185 d_main_gui->SetFrequencyAxis(min, max);
189 qtgui_sink_c::fft(const gr_complex *data_in, int size, gr_complex *data_out)
191 if (d_window.size()) {
192 gr_complex *dst = d_fft->get_inbuf();
194 for (i = 0; i < size; i++) // apply window
195 dst[i] = data_in[i] * d_window[i];
198 memcpy (d_fft->get_inbuf(), data_in, sizeof(gr_complex)*size);
201 d_fft->execute (); // compute the fft
203 for(int i=0; i < size; i++) {
204 d_fft->get_outbuf()[i] /= size;
207 // copy result to our output
208 if(d_shift) { // apply a fft shift on the data
209 unsigned int len = (unsigned int)(ceil(size/2.0));
210 memcpy(&data_out[0], &d_fft->get_outbuf()[len], sizeof(gr_complex)*(size - len));
211 memcpy(&data_out[size - len], &d_fft->get_outbuf()[0], sizeof(gr_complex)*len);
214 memcpy(data_out, d_fft->get_outbuf(), sizeof(gr_complex)*size);
219 qtgui_sink_c::windowreset()
221 gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();
222 if(d_wintype != newwintype) {
223 d_wintype = newwintype;
229 qtgui_sink_c::buildwindow()
233 d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
238 qtgui_sink_c::fftresize()
240 int newfftsize = d_main_gui->GetFFTSize();
242 if(newfftsize != d_fftsize) {
244 // Resize the fftdata buffer; no need to preserve old data
246 d_fftdata = new gr_complex[newfftsize];
248 // Resize residbuf and replace data
249 delete [] d_residbuf;
250 d_residbuf = new gr_complex[newfftsize];
252 // Set new fft size and reset buffer index
253 // (throws away any currently held data, but who cares?)
254 d_fftsize = newfftsize;
257 // Reset window to reflect new size
260 // Reset FFTW plan for new size
262 d_fft = new gri_fft_complex (d_fftsize, true);
268 qtgui_sink_c::general_work (int noutput_items,
269 gr_vector_int &ninput_items,
270 gr_vector_const_void_star &input_items,
271 gr_vector_void_star &output_items)
274 const gr_complex *in = (const gr_complex*)input_items[0];
276 pthread_mutex_lock(&d_pmutex);
278 // Update the FFT size from the application
282 const timespec currentTime = get_highres_clock();
283 const timespec lastUpdateGUITime = d_main_gui->GetLastGUIUpdateTime();
285 if(diff_timespec(currentTime, lastUpdateGUITime) > 0.25) {
288 int filler = std::min(d_fftsize - d_index, noutput_items);
290 memcpy(&d_residbuf[d_index], &in[0], sizeof(gr_complex)*filler);
296 if(d_index == d_fftsize) {
298 fft(d_residbuf, d_fftsize, d_fftdata);
300 d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize, NULL, 0,
301 (float*)d_residbuf, d_fftsize,
302 1.0/4.0, convert_to_timespec(0.0), true);
305 for(; i < noutput_items; i+=d_fftsize) {
306 if(noutput_items - i > d_fftsize) {
308 fft(&in[i], d_fftsize, d_fftdata);
310 d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize, NULL, 0,
311 (float*)&in[i], d_fftsize,
312 1.0/4.0, convert_to_timespec(0.0), true);
316 if(noutput_items > j) {
317 d_index = noutput_items - j;
318 memcpy(d_residbuf, &in[j], sizeof(gr_complex)*d_index);
322 pthread_mutex_unlock(&d_pmutex);
324 consume_each(noutput_items);
325 return noutput_items;