Removing Waterfall3DPlot. The qwt_plot3d is too much of a hassle to deal with and...
[debian/gnuradio] / gr-qtgui / src / lib / qtgui_sink_f.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009,2010 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 gnuradio::get_initial_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_plottime(plottime), d_plotconst(plotconst),
68     d_parent(parent)
69 {
70   if(plotwaterfall3d == true) {
71     fprintf(stderr, "Warning: plotting Waterfall3D has been removed; enabling plotwaterfall3d has no effect.\n");
72   }
73
74   d_main_gui = NULL;
75   pthread_mutex_init(&d_pmutex, NULL);
76   lock();
77
78   // Perform fftshift operation;
79   // this is usually desired when plotting
80   d_shift = true;
81
82   d_fft = new gri_fft_complex (d_fftsize, true);
83
84   d_index = 0;
85   d_residbuf = new float[d_fftsize];
86
87   buildwindow();
88
89   initialize(use_openGL);
90 }
91
92 qtgui_sink_f::~qtgui_sink_f()
93 {
94   delete d_main_gui;
95   delete [] d_residbuf;
96   delete d_fft;
97 }
98
99 void
100 qtgui_sink_f::forecast(int noutput_items, gr_vector_int &ninput_items_required)
101 {
102   unsigned int ninputs = ninput_items_required.size();
103   for (unsigned int i = 0; i < ninputs; i++) {
104     ninput_items_required[i] = std::min(d_fftsize, 8191);
105   }
106 }
107
108 void qtgui_sink_f::lock()
109 {
110   pthread_mutex_lock(&d_pmutex);
111 }
112
113 void qtgui_sink_f::unlock()
114 {
115   pthread_mutex_unlock(&d_pmutex);
116 }
117
118 void
119 qtgui_sink_f::initialize(const bool opengl)
120 {
121   if(qApp != NULL) {
122     d_qApplication = qApp;
123   }
124   else {
125     int argc;
126     char **argv = NULL;
127     d_qApplication = new QApplication(argc, argv);
128   }
129
130
131   uint64_t maxBufferSize = 32768;
132   d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize,
133                                     d_center_freq, 
134                                     -d_bandwidth/2.0, 
135                                     d_bandwidth/2.0);
136   d_main_gui->SetDisplayTitle(d_name);
137   d_main_gui->SetFFTSize(d_fftsize);
138   d_main_gui->SetWindowType((int)d_wintype);
139
140   d_main_gui->OpenSpectrumWindow(d_parent,
141                                  d_plotfreq, d_plotwaterfall,
142                                  d_plottime, d_plotconst,
143                                  opengl);
144
145   // initialize update time to 10 times a second
146   set_update_time(0.1);
147
148   d_object = new qtgui_obj(d_qApplication);
149   qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
150 }
151
152 void
153 qtgui_sink_f::exec_()
154 {
155   d_qApplication->exec();
156 }
157
158 QWidget*
159 qtgui_sink_f::qwidget()
160 {
161   return d_main_gui->qwidget();
162 }
163
164 PyObject*
165 qtgui_sink_f::pyqwidget()
166 {
167   PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
168   PyObject *retarg = Py_BuildValue("N", w);
169   return retarg;
170 }
171
172 void
173 qtgui_sink_f::set_frequency_range(const double centerfreq, 
174                                   const double bandwidth)
175 {
176   d_center_freq = centerfreq;
177   d_bandwidth = bandwidth;
178   d_main_gui->SetFrequencyRange(d_center_freq, 
179                                 -d_bandwidth/2.0,
180                                 d_bandwidth/2.0);
181 }
182
183 void
184 qtgui_sink_f::set_time_domain_axis(double min, double max)
185 {
186   d_main_gui->SetTimeDomainAxis(min, max);
187 }
188
189 void
190 qtgui_sink_f::set_constellation_axis(double xmin, double xmax,
191                                      double ymin, double ymax)
192 {
193   d_main_gui->SetConstellationAxis(xmin, xmax, ymin, ymax);
194 }
195
196 void 
197 qtgui_sink_f::set_constellation_pen_size(int size)
198 {
199   d_main_gui->SetConstellationPenSize(size);
200 }
201
202
203 void
204 qtgui_sink_f::set_frequency_axis(double min, double max)
205 {
206   d_main_gui->SetFrequencyAxis(min, max);
207 }
208
209 void
210 qtgui_sink_f::set_update_time(double t)
211 {
212   d_update_time = t;
213   d_main_gui->SetUpdateTime(d_update_time);
214 }
215
216 void
217 qtgui_sink_f::fft(const float *data_in, int size)
218 {
219   if (d_window.size()) {
220     gr_complex *dst = d_fft->get_inbuf();
221     for (int i = 0; i < size; i++)              // apply window
222       dst[i] = data_in[i] * d_window[i];
223   }
224   else {
225       gr_complex *dst = d_fft->get_inbuf();
226       for (int i = 0; i < size; i++)            // float to complex conversion
227         dst[i] = data_in[i];
228   }
229   
230   d_fft->execute ();     // compute the fft
231 }
232
233 void 
234 qtgui_sink_f::windowreset()
235 {
236   gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();  
237   if(d_wintype != newwintype) {
238     d_wintype = newwintype;
239     buildwindow();
240   }
241 }
242
243 void
244 qtgui_sink_f::buildwindow()
245 {
246   d_window.clear();
247   if(d_wintype != 0) {
248     d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
249   }
250 }
251
252 void
253 qtgui_sink_f::fftresize()
254 {
255   int newfftsize = d_main_gui->GetFFTSize();
256
257   if(newfftsize != d_fftsize) {
258
259     // Resize residbuf and replace data
260     delete [] d_residbuf;
261     d_residbuf = new float[newfftsize];
262
263     // Set new fft size and reset buffer index 
264     // (throws away any currently held data, but who cares?) 
265     d_fftsize = newfftsize;
266     d_index = 0;
267     
268     // Reset window to reflect new size
269     buildwindow();
270
271     // Reset FFTW plan for new size
272     delete d_fft;
273     d_fft = new gri_fft_complex (d_fftsize, true);
274   }
275 }
276
277
278 int
279 qtgui_sink_f::general_work (int noutput_items,
280                             gr_vector_int &ninput_items,
281                             gr_vector_const_void_star &input_items,
282                             gr_vector_void_star &output_items)
283 {
284   int j=0;
285   const float *in = (const float*)input_items[0];
286
287   pthread_mutex_lock(&d_pmutex);
288
289   // Update the FFT size from the application
290   fftresize();
291   windowreset();
292
293   for(int i=0; i < noutput_items; i+=d_fftsize) {
294     unsigned int datasize = noutput_items - i;
295     unsigned int resid = d_fftsize-d_index;
296
297     // If we have enough input for one full FFT, do it
298     if(datasize >= resid) {
299       const timespec currentTime = get_highres_clock();
300       
301       // Fill up residbuf with d_fftsize number of items
302       memcpy(d_residbuf+d_index, &in[j], sizeof(float)*resid);
303       d_index = 0;
304
305       j += resid;
306       fft(d_residbuf, d_fftsize);
307       
308       d_main_gui->UpdateWindow(true, d_fft->get_outbuf(), d_fftsize,
309                                (float*)d_residbuf, d_fftsize, NULL, 0,
310                                currentTime, true);
311     }
312     // Otherwise, copy what we received into the residbuf for next time
313     else {
314       memcpy(d_residbuf+d_index, &in[j], sizeof(float)*datasize);
315       d_index += datasize;
316       j += datasize;
317     }   
318   }
319
320   pthread_mutex_unlock(&d_pmutex);
321
322   consume_each(j);
323   return j;
324 }