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_f.h>
28 #include <gr_io_signature.h>
34 qtgui_make_sink_f (int fftsize, int wintype,
36 const std::string &name,
37 bool plotfreq, bool plotwaterfall,
38 bool plotwaterfall3d, bool plottime,
43 return qtgui_sink_f_sptr (new qtgui_sink_f (fftsize, wintype,
45 plotfreq, plotwaterfall,
46 plotwaterfall3d, plottime,
52 qtgui_sink_f::qtgui_sink_f (int fftsize, int wintype,
54 const std::string &name,
55 bool plotfreq, bool plotwaterfall,
56 bool plotwaterfall3d, bool plottime,
61 gr_make_io_signature (1, 1, sizeof(float)),
62 gr_make_io_signature (0, 0, 0)),
64 d_wintype((gr_firdes::win_type)(wintype)),
65 d_center_freq(fc), d_bandwidth(bw), 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);
82 d_residbuf = new float[d_fftsize];
86 initialize(use_openGL);
89 qtgui_sink_f::~qtgui_sink_f()
97 qtgui_sink_f::forecast(int noutput_items, gr_vector_int &ninput_items_required)
99 unsigned int ninputs = ninput_items_required.size();
100 for (unsigned int i = 0; i < ninputs; i++) {
101 ninput_items_required[i] = std::min(d_fftsize, 8191);
105 void qtgui_sink_f::lock()
107 pthread_mutex_lock(&d_pmutex);
110 void qtgui_sink_f::unlock()
112 pthread_mutex_unlock(&d_pmutex);
116 qtgui_sink_f::initialize(const bool opengl)
119 d_qApplication = qApp;
124 d_qApplication = new QApplication(argc, argv);
128 uint64_t maxBufferSize = 32768;
129 d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize,
133 d_main_gui->SetDisplayTitle(d_name);
134 d_main_gui->SetFFTSize(d_fftsize);
135 d_main_gui->SetWindowType((int)d_wintype);
137 d_main_gui->OpenSpectrumWindow(d_parent,
138 d_plotfreq, d_plotwaterfall,
139 d_plotwaterfall3d, d_plottime,
143 // initialize update time to 10 times a second
144 set_update_time(0.1);
146 d_object = new qtgui_obj(d_qApplication);
147 qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
151 qtgui_sink_f::exec_()
153 d_qApplication->exec();
157 qtgui_sink_f::qwidget()
159 return d_main_gui->qwidget();
163 qtgui_sink_f::pyqwidget()
165 PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
166 PyObject *retarg = Py_BuildValue("N", w);
171 qtgui_sink_f::set_frequency_range(const double centerfreq,
172 const double bandwidth)
174 d_center_freq = centerfreq;
175 d_bandwidth = bandwidth;
176 d_main_gui->SetFrequencyRange(d_center_freq,
182 qtgui_sink_f::set_time_domain_axis(double min, double max)
184 d_main_gui->SetTimeDomainAxis(min, max);
188 qtgui_sink_f::set_constellation_axis(double xmin, double xmax,
189 double ymin, double ymax)
191 d_main_gui->SetConstellationAxis(xmin, xmax, ymin, ymax);
195 qtgui_sink_f::set_constellation_pen_size(int size)
197 d_main_gui->SetConstellationPenSize(size);
202 qtgui_sink_f::set_frequency_axis(double min, double max)
204 d_main_gui->SetFrequencyAxis(min, max);
208 qtgui_sink_f::set_update_time(double t)
211 d_main_gui->SetUpdateTime(d_update_time);
215 qtgui_sink_f::fft(const float *data_in, int size)
217 if (d_window.size()) {
218 gr_complex *dst = d_fft->get_inbuf();
219 for (int i = 0; i < size; i++) // apply window
220 dst[i] = data_in[i] * d_window[i];
223 gr_complex *dst = d_fft->get_inbuf();
224 for (int i = 0; i < size; i++) // float to complex conversion
228 d_fft->execute (); // compute the fft
232 qtgui_sink_f::windowreset()
234 gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();
235 if(d_wintype != newwintype) {
236 d_wintype = newwintype;
242 qtgui_sink_f::buildwindow()
246 d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
251 qtgui_sink_f::fftresize()
253 int newfftsize = d_main_gui->GetFFTSize();
255 if(newfftsize != d_fftsize) {
257 // Resize residbuf and replace data
258 delete [] d_residbuf;
259 d_residbuf = new float[newfftsize];
261 // Set new fft size and reset buffer index
262 // (throws away any currently held data, but who cares?)
263 d_fftsize = newfftsize;
266 // Reset window to reflect new size
269 // Reset FFTW plan for new size
271 d_fft = new gri_fft_complex (d_fftsize, true);
277 qtgui_sink_f::general_work (int noutput_items,
278 gr_vector_int &ninput_items,
279 gr_vector_const_void_star &input_items,
280 gr_vector_void_star &output_items)
283 const float *in = (const float*)input_items[0];
285 pthread_mutex_lock(&d_pmutex);
287 // Update the FFT size from the application
291 for(int i=0; i < noutput_items; i+=d_fftsize) {
292 unsigned int datasize = noutput_items - i;
293 unsigned int resid = d_fftsize-d_index;
295 // If we have enough input for one full FFT, do it
296 if(datasize >= resid) {
297 const timespec currentTime = get_highres_clock();
299 // Fill up residbuf with d_fftsize number of items
300 memcpy(d_residbuf+d_index, &in[j], sizeof(float)*resid);
304 fft(d_residbuf, d_fftsize);
306 d_main_gui->UpdateWindow(true, d_fft->get_outbuf(), d_fftsize,
307 (float*)d_residbuf, d_fftsize, NULL, 0,
310 // Otherwise, copy what we received into the residbuf for next time
312 memcpy(d_residbuf+d_index, &in[j], sizeof(float)*datasize);
318 pthread_mutex_unlock(&d_pmutex);