Imported Upstream version 3.2.2
[debian/gnuradio] / gr-qtgui / src / lib / qtgui_sink_c.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <qtgui_sink_c.h>
28 #include <gr_io_signature.h>
29 #include <string.h>
30
31 #include <QTimer>
32
33 qtgui_sink_c_sptr
34 qtgui_make_sink_c (int fftsize, int wintype,
35                    double fc, double bw,
36                    const std::string &name,
37                    bool plotfreq, bool plotwaterfall,
38                    bool plotwaterfall3d, bool plottime,
39                    bool plotconst,
40                    bool use_openGL,
41                    QWidget *parent)
42 {
43   return qtgui_sink_c_sptr (new qtgui_sink_c (fftsize, wintype,
44                                               fc, bw, name,
45                                               plotfreq, plotwaterfall,
46                                               plotwaterfall3d, plottime,
47                                               plotconst,
48                                               use_openGL,
49                                               parent));
50 }
51
52 qtgui_sink_c::qtgui_sink_c (int fftsize, int wintype,
53                             double fc, double bw,
54                             const std::string &name,
55                             bool plotfreq, bool plotwaterfall,
56                             bool plotwaterfall3d, bool plottime,
57                             bool plotconst,
58                             bool use_openGL,
59                             QWidget *parent)
60   : gr_block ("sink_c",
61               gr_make_io_signature (1, -1, sizeof(gr_complex)),
62               gr_make_io_signature (0, 0, 0)),
63     d_fftsize(fftsize),
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),
69     d_parent(parent)
70 {
71   d_main_gui = NULL;
72   pthread_mutex_init(&d_pmutex, NULL);
73   lock();
74
75   // Perform fftshift operation;
76   // this is usually desired when plotting
77   d_shift = true;  
78
79   d_fft = new gri_fft_complex (d_fftsize, true);
80
81   d_fftdata = new gr_complex[d_fftsize];
82
83   d_index = 0;
84   d_residbuf = new gr_complex[d_fftsize];
85
86   buildwindow();
87
88   initialize(use_openGL);
89 }
90
91 qtgui_sink_c::~qtgui_sink_c()
92 {
93   delete d_object;
94   delete [] d_fftdata;
95   delete [] d_residbuf;
96   delete d_fft;
97 }
98
99 void qtgui_sink_c::lock()
100 {
101   pthread_mutex_lock(&d_pmutex);
102 }
103
104 void qtgui_sink_c::unlock()
105 {
106   pthread_mutex_unlock(&d_pmutex);
107 }
108
109
110 void
111 qtgui_sink_c::initialize(const bool opengl)
112 {
113   if(qApp != NULL) {
114     d_qApplication = qApp;
115   }
116   else {
117     int argc;
118     char **argv = NULL;
119     d_qApplication = new QApplication(argc, argv);
120   }
121
122   if(d_center_freq < 0) {
123     throw std::runtime_error("qtgui_sink_c: Received bad center frequency.\n");
124   }
125
126   uint64_t maxBufferSize = 32768;
127   d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize, 
128                                     d_center_freq, 
129                                     -d_bandwidth/2.0, 
130                                     d_bandwidth/2.0);
131
132   d_main_gui->SetDisplayTitle(d_name);
133   d_main_gui->SetFFTSize(d_fftsize);
134   d_main_gui->SetWindowType((int)d_wintype);
135
136   d_main_gui->OpenSpectrumWindow(d_parent, 
137                                  d_plotfreq, d_plotwaterfall,
138                                  d_plotwaterfall3d, d_plottime,
139                                  d_plotconst,
140                                  opengl);
141
142   d_object = new qtgui_obj(d_qApplication);
143   qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
144 }
145
146
147 void
148 qtgui_sink_c::exec_()
149 {
150   d_qApplication->exec();
151 }
152
153 QWidget*
154 qtgui_sink_c::qwidget()
155 {
156   return d_main_gui->qwidget();
157 }
158
159 PyObject*
160 qtgui_sink_c::pyqwidget()
161 {
162   PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
163   PyObject *retarg = Py_BuildValue("N", w);
164   return retarg;
165 }
166
167 void
168 qtgui_sink_c::set_frequency_range(const double centerfreq, 
169                                   const double bandwidth)
170 {
171   d_center_freq = centerfreq;
172   d_bandwidth = bandwidth;
173   d_main_gui->SetFrequencyRange(d_center_freq, 
174                                 -d_bandwidth/2.0,
175                                 d_bandwidth/2.0);
176 }
177
178 void
179 qtgui_sink_c::set_time_domain_axis(double min, double max)
180 {
181   d_main_gui->SetTimeDomainAxis(min, max);
182 }
183
184 void
185 qtgui_sink_c::set_constellation_axis(double xmin, double xmax,
186                                      double ymin, double ymax)
187 {
188   d_main_gui->SetConstellationAxis(xmin, xmax, ymin, ymax);
189 }
190
191 void
192 qtgui_sink_c::set_frequency_axis(double min, double max)
193 {
194   d_main_gui->SetFrequencyAxis(min, max);
195 }
196
197 void
198 qtgui_sink_c::fft(const gr_complex *data_in, int size, gr_complex *data_out)
199 {
200   if (d_window.size()) {
201     gr_complex *dst = d_fft->get_inbuf();
202     int i;
203     for (i = 0; i < size; i++)          // apply window
204       dst[i] = data_in[i] * d_window[i];
205   }
206   else {
207     memcpy (d_fft->get_inbuf(), data_in, sizeof(gr_complex)*size);
208   }
209
210   d_fft->execute ();     // compute the fft
211
212   for(int i=0; i < size; i++) {
213     d_fft->get_outbuf()[i] /= size;
214   }
215
216   // copy result to our output
217   if(d_shift) {  // apply a fft shift on the data
218     unsigned int len = (unsigned int)(ceil(size/2.0));
219     memcpy(&data_out[0], &d_fft->get_outbuf()[len], sizeof(gr_complex)*(size - len));
220     memcpy(&data_out[size - len], &d_fft->get_outbuf()[0], sizeof(gr_complex)*len);
221   }
222   else {
223     memcpy(data_out, d_fft->get_outbuf(), sizeof(gr_complex)*size);
224   }
225 }
226
227 void 
228 qtgui_sink_c::windowreset()
229 {
230   gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();  
231   if(d_wintype != newwintype) {
232     d_wintype = newwintype;
233     buildwindow();
234   }
235 }
236
237 void
238 qtgui_sink_c::buildwindow()
239 {
240   d_window.clear();
241   if(d_wintype != 0) {
242     d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
243   }
244 }
245
246 void
247 qtgui_sink_c::fftresize()
248 {
249   int newfftsize = d_main_gui->GetFFTSize();
250
251   if(newfftsize != d_fftsize) {
252
253     // Resize the fftdata buffer; no need to preserve old data
254     delete [] d_fftdata;
255     d_fftdata = new gr_complex[newfftsize];
256
257     // Resize residbuf and replace data
258     delete [] d_residbuf;
259     d_residbuf = new gr_complex[newfftsize];
260
261     // Set new fft size and reset buffer index 
262     // (throws away any currently held data, but who cares?) 
263     d_fftsize = newfftsize;
264     d_index = 0;
265     
266     // Reset window to reflect new size
267     buildwindow();
268
269     // Reset FFTW plan for new size
270     delete d_fft;
271     d_fft = new gri_fft_complex (d_fftsize, true);
272   }
273 }
274
275
276 int
277 qtgui_sink_c::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)
281 {
282   int i=0, j=0;
283   const gr_complex *in = (const gr_complex*)input_items[0];
284
285   pthread_mutex_lock(&d_pmutex);
286
287   // Update the FFT size from the application
288   fftresize();
289   windowreset();
290  
291   const timespec currentTime = get_highres_clock();
292   const timespec lastUpdateGUITime = d_main_gui->GetLastGUIUpdateTime();
293
294   if(diff_timespec(currentTime, lastUpdateGUITime) > 0.05) {
295
296     if(d_index) {
297       int filler = std::min(d_fftsize - d_index, noutput_items);
298       
299       memcpy(&d_residbuf[d_index], &in[0], sizeof(gr_complex)*filler);
300       d_index += filler;
301       i = filler;
302       j = filler;
303     }
304     
305     if(d_index == d_fftsize) {
306       d_index = 0;
307       fft(d_residbuf, d_fftsize, d_fftdata);
308       
309       d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize, NULL, 0, 
310                                (float*)d_residbuf, d_fftsize,
311                                1.0/4.0, convert_to_timespec(0.0), true);
312     }
313     
314     for(; i < noutput_items; i+=d_fftsize) {
315       if(noutput_items - i > d_fftsize) {
316         j += d_fftsize;
317         fft(&in[i], d_fftsize, d_fftdata);
318         
319         d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize, NULL, 0, 
320                                  (float*)&in[i], d_fftsize,
321                                  1.0/4.0, convert_to_timespec(0.0), true);
322       }
323     }
324     
325     if(noutput_items > j) {
326       d_index = noutput_items - j;
327       memcpy(d_residbuf, &in[j], sizeof(gr_complex)*d_index);
328     }
329   }
330
331   pthread_mutex_unlock(&d_pmutex);
332
333   consume_each(noutput_items);
334   return noutput_items;
335 }