3 * Copyright 2008,2009,2010 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,
36 const std::string &name,
37 bool plotfreq, bool plotwaterfall,
38 bool plotwaterfall3d, bool plottime,
43 return gnuradio::get_initial_sptr(new qtgui_sink_c (fftsize, wintype,
45 plotfreq, plotwaterfall,
46 plotwaterfall3d, plottime,
52 qtgui_sink_c::qtgui_sink_c (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(gr_complex)),
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 gr_complex[d_fftsize];
86 initialize(use_openGL);
89 qtgui_sink_c::~qtgui_sink_c()
97 qtgui_sink_c::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_c::lock()
107 pthread_mutex_lock(&d_pmutex);
110 void qtgui_sink_c::unlock()
112 pthread_mutex_unlock(&d_pmutex);
117 qtgui_sink_c::initialize(const bool opengl)
120 d_qApplication = qApp;
125 d_qApplication = new QApplication(argc, argv);
128 if(d_center_freq < 0) {
129 throw std::runtime_error("qtgui_sink_c: Received bad center frequency.\n");
132 uint64_t maxBufferSize = 32768;
133 d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize,
138 d_main_gui->SetDisplayTitle(d_name);
139 d_main_gui->SetFFTSize(d_fftsize);
140 d_main_gui->SetWindowType((int)d_wintype);
142 d_main_gui->OpenSpectrumWindow(d_parent,
143 d_plotfreq, d_plotwaterfall,
144 d_plotwaterfall3d, d_plottime,
148 // initialize update time to 10 times a second
149 set_update_time(0.1);
151 d_object = new qtgui_obj(d_qApplication);
152 qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
157 qtgui_sink_c::exec_()
159 d_qApplication->exec();
163 qtgui_sink_c::qwidget()
165 return d_main_gui->qwidget();
169 qtgui_sink_c::pyqwidget()
171 PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
172 PyObject *retarg = Py_BuildValue("N", w);
177 qtgui_sink_c::set_frequency_range(const double centerfreq,
178 const double bandwidth)
180 d_center_freq = centerfreq;
181 d_bandwidth = bandwidth;
182 d_main_gui->SetFrequencyRange(d_center_freq,
188 qtgui_sink_c::set_time_domain_axis(double min, double max)
190 d_main_gui->SetTimeDomainAxis(min, max);
194 qtgui_sink_c::set_constellation_axis(double xmin, double xmax,
195 double ymin, double ymax)
197 d_main_gui->SetConstellationAxis(xmin, xmax, ymin, ymax);
201 qtgui_sink_c::set_constellation_pen_size(int size)
203 d_main_gui->SetConstellationPenSize(size);
208 qtgui_sink_c::set_frequency_axis(double min, double max)
210 d_main_gui->SetFrequencyAxis(min, max);
214 qtgui_sink_c::set_update_time(double t)
217 d_main_gui->SetUpdateTime(d_update_time);
221 qtgui_sink_c::fft(const gr_complex *data_in, int size)
223 if (d_window.size()) {
224 gr_complex *dst = d_fft->get_inbuf();
226 for (i = 0; i < size; i++) // apply window
227 dst[i] = data_in[i] * d_window[i];
230 memcpy (d_fft->get_inbuf(), data_in, sizeof(gr_complex)*size);
233 d_fft->execute (); // compute the fft
237 qtgui_sink_c::windowreset()
239 gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();
240 if(d_wintype != newwintype) {
241 d_wintype = newwintype;
247 qtgui_sink_c::buildwindow()
251 d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
256 qtgui_sink_c::fftresize()
258 int newfftsize = d_main_gui->GetFFTSize();
260 if(newfftsize != d_fftsize) {
262 // Resize residbuf and replace data
263 delete [] d_residbuf;
264 d_residbuf = new gr_complex[newfftsize];
266 // Set new fft size and reset buffer index
267 // (throws away any currently held data, but who cares?)
268 d_fftsize = newfftsize;
271 // Reset window to reflect new size
274 // Reset FFTW plan for new size
276 d_fft = new gri_fft_complex (d_fftsize, true);
282 qtgui_sink_c::general_work (int noutput_items,
283 gr_vector_int &ninput_items,
284 gr_vector_const_void_star &input_items,
285 gr_vector_void_star &output_items)
288 const gr_complex *in = (const gr_complex*)input_items[0];
290 pthread_mutex_lock(&d_pmutex);
292 // Update the FFT size from the application
296 for(int i=0; i < noutput_items; i+=d_fftsize) {
297 unsigned int datasize = noutput_items - i;
298 unsigned int resid = d_fftsize-d_index;
300 // If we have enough input for one full FFT, do it
301 if(datasize >= resid) {
302 const timespec currentTime = get_highres_clock();
304 // Fill up residbuf with d_fftsize number of items
305 memcpy(d_residbuf+d_index, &in[j], sizeof(gr_complex)*resid);
309 fft(d_residbuf, d_fftsize);
311 d_main_gui->UpdateWindow(true, d_fft->get_outbuf(), d_fftsize,
312 NULL, 0, (float*)d_residbuf, d_fftsize,
315 // Otherwise, copy what we received into the residbuf for next time
317 memcpy(d_residbuf+d_index, &in[j], sizeof(gr_complex)*datasize);
323 pthread_mutex_unlock(&d_pmutex);