Imported Upstream version 3.2.2
[debian/gnuradio] / gr-qtgui / src / lib / qtgui_sink_f.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_f.h>
28 #include <gr_io_signature.h>
29 #include <string.h>
30
31 #include <QTimer>
32
33 qtgui_sink_f_sptr
34 qtgui_make_sink_f (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_f_sptr (new qtgui_sink_f (fftsize, wintype,
44                                               fc, bw, name,
45                                               plotfreq, plotwaterfall,
46                                               plotwaterfall3d, plottime,
47                                               plotconst,
48                                               use_openGL,
49                                               parent));
50 }
51
52 qtgui_sink_f::qtgui_sink_f (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_f",
61               gr_make_io_signature (1, 1, sizeof(float)),
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 float[d_fftsize];
85
86   buildwindow();
87
88   initialize(use_openGL);
89 }
90
91 qtgui_sink_f::~qtgui_sink_f()
92 {
93   delete d_object;
94   delete [] d_fftdata;
95   delete [] d_residbuf;
96   delete d_fft;
97 }
98
99 void qtgui_sink_f::lock()
100 {
101   pthread_mutex_lock(&d_pmutex);
102 }
103
104 void qtgui_sink_f::unlock()
105 {
106   pthread_mutex_unlock(&d_pmutex);
107 }
108
109 void
110 qtgui_sink_f::initialize(const bool opengl)
111 {
112   if(qApp != NULL) {
113     d_qApplication = qApp;
114   }
115   else {
116     int argc;
117     char **argv = NULL;
118     d_qApplication = new QApplication(argc, argv);
119   }
120
121
122   uint64_t maxBufferSize = 32768;
123   d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize,
124                                     d_center_freq, 
125                                     -d_bandwidth/2.0, 
126                                     d_bandwidth/2.0);
127   d_main_gui->SetDisplayTitle(d_name);
128   d_main_gui->SetFFTSize(d_fftsize);
129   d_main_gui->SetWindowType((int)d_wintype);
130
131   d_main_gui->OpenSpectrumWindow(d_parent,
132                                  d_plotfreq, d_plotwaterfall,
133                                  d_plotwaterfall3d, d_plottime,
134                                  d_plotconst,
135                                  opengl);
136
137   d_object = new qtgui_obj(d_qApplication);
138   qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
139 }
140
141 void
142 qtgui_sink_f::exec_()
143 {
144   d_qApplication->exec();
145 }
146
147 QWidget*
148 qtgui_sink_f::qwidget()
149 {
150   return d_main_gui->qwidget();
151 }
152
153 PyObject*
154 qtgui_sink_f::pyqwidget()
155 {
156   PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
157   PyObject *retarg = Py_BuildValue("N", w);
158   return retarg;
159 }
160
161 void
162 qtgui_sink_f::set_frequency_range(const double centerfreq, 
163                                   const double bandwidth)
164 {
165   d_center_freq = centerfreq;
166   d_bandwidth = bandwidth;
167   d_main_gui->SetFrequencyRange(d_center_freq, 
168                                 -d_bandwidth/2.0,
169                                 d_bandwidth/2.0);
170 }
171
172 void
173 qtgui_sink_f::set_time_domain_axis(double min, double max)
174 {
175   d_main_gui->SetTimeDomainAxis(min, max);
176 }
177
178 void
179 qtgui_sink_f::set_constellation_axis(double xmin, double xmax,
180                                      double ymin, double ymax)
181 {
182   d_main_gui->SetConstellationAxis(xmin, xmax, ymin, ymax);
183 }
184
185 void
186 qtgui_sink_f::set_frequency_axis(double min, double max)
187 {
188   d_main_gui->SetFrequencyAxis(min, max);
189 }
190
191 void
192 qtgui_sink_f::fft(const float *data_in, int size, gr_complex *data_out)
193 {
194   if (d_window.size()) {
195     gr_complex *dst = d_fft->get_inbuf();
196     for (int i = 0; i < size; i++)              // apply window
197       dst[i] = data_in[i] * d_window[i];
198   }
199   else {
200       gr_complex *dst = d_fft->get_inbuf();
201       for (int i = 0; i < size; i++)            // float to complex conversion
202         dst[i] = data_in[i];
203   }
204   
205   d_fft->execute ();     // compute the fft
206
207   for(int i=0; i < size; i++) {
208     d_fft->get_outbuf()[i] /= size;
209   }
210
211   // copy result to our output
212   if(d_shift) {  // apply a fft shift on the data
213     unsigned int len = (unsigned int)(ceil(size/2.0));
214     memcpy(&data_out[0], &d_fft->get_outbuf()[len], sizeof(gr_complex)*(size - len));
215     memcpy(&data_out[size - len], &d_fft->get_outbuf()[0], sizeof(gr_complex)*len);
216   }
217   else {
218     memcpy(data_out, d_fft->get_outbuf(), sizeof(gr_complex)*size);
219   }
220 }
221
222 void 
223 qtgui_sink_f::windowreset()
224 {
225   gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();  
226   if(d_wintype != newwintype) {
227     d_wintype = newwintype;
228     buildwindow();
229   }
230 }
231
232 void
233 qtgui_sink_f::buildwindow()
234 {
235   d_window.clear();
236   if(d_wintype != 0) {
237     d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
238   }
239 }
240
241 void
242 qtgui_sink_f::fftresize()
243 {
244   int newfftsize = d_main_gui->GetFFTSize();
245
246   if(newfftsize != d_fftsize) {
247
248     // Resize the fftdata buffer; no need to preserve old data
249     delete [] d_fftdata;
250     d_fftdata = new gr_complex[newfftsize];
251
252     // Resize residbuf and replace data
253     delete [] d_residbuf;
254     d_residbuf = new float[newfftsize];
255
256     // Set new fft size and reset buffer index 
257     // (throws away any currently held data, but who cares?) 
258     d_fftsize = newfftsize;
259     d_index = 0;
260     
261     // Reset window to reflect new size
262     buildwindow();
263
264     // Reset FFTW plan for new size
265     delete d_fft;
266     d_fft = new gri_fft_complex (d_fftsize, true);
267   }
268 }
269
270
271 int
272 qtgui_sink_f::general_work (int noutput_items,
273                             gr_vector_int &ninput_items,
274                             gr_vector_const_void_star &input_items,
275                             gr_vector_void_star &output_items)
276 {
277   int i=0, j=0;
278   const float *in = (const float*)input_items[0];
279
280   pthread_mutex_lock(&d_pmutex);
281
282   if(d_index) {
283     int filler = std::min(d_fftsize - d_index, noutput_items);
284     memcpy(&d_residbuf[d_index], &in[0], sizeof(float)*filler);
285     d_index += filler;
286     i = filler;
287     j = filler;
288   }
289
290   if(d_index == d_fftsize) {
291     d_index = 0;
292     fft(d_residbuf, d_fftsize, d_fftdata);
293     
294     d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize,
295                              d_residbuf, d_fftsize, NULL, 0,
296                              1.0/4.0, convert_to_timespec(0.0), true);
297   }
298   
299   for(; i < noutput_items; i+=d_fftsize) {
300     if(noutput_items - i > d_fftsize) {
301       j += d_fftsize;
302       fft(&in[i], d_fftsize, d_fftdata);
303       
304       d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize, &in[i],
305                                d_fftsize, NULL, 0, 1.0/4.0,
306                                convert_to_timespec(0.0), true);
307     }
308   }
309
310   if(noutput_items > j) {
311     d_index = noutput_items - j;
312     memcpy(d_residbuf, &in[j], sizeof(float)*d_index);
313   }
314
315   pthread_mutex_unlock(&d_pmutex);
316
317   consume_each(noutput_items);
318   return noutput_items;
319 }