Merging qtdevel2 branch -r10565:10849. This adds a lot of fixes and capabilities...
[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                    float fmin, float fmax,
36                    const std::string &name,
37                    bool plotfreq, bool plotwaterfall,
38                    bool plotwaterfall3d, bool plottime,
39                    bool plotconst,
40                    QWidget *parent)
41 {
42   return qtgui_sink_f_sptr (new qtgui_sink_f (fftsize, wintype,
43                                               fmin, fmax, name,
44                                               plotfreq, plotwaterfall,
45                                               plotwaterfall3d, plottime,
46                                               plotconst,
47                                               parent));
48 }
49
50 qtgui_sink_f::qtgui_sink_f (int fftsize, int wintype,
51                             float fmin, float fmax,
52                             const std::string &name,
53                             bool plotfreq, bool plotwaterfall,
54                             bool plotwaterfall3d, bool plottime,
55                             bool plotconst,
56                             QWidget *parent)
57   : gr_block ("sink_f",
58               gr_make_io_signature (1, 1, sizeof(float)),
59               gr_make_io_signature (0, 0, 0)),
60     d_fftsize(fftsize),
61     d_wintype((gr_firdes::win_type)(wintype)),
62     d_fmin(fmin), d_fmax(fmax), d_name(name),
63     d_plotfreq(plotfreq), d_plotwaterfall(plotwaterfall),
64     d_plotwaterfall3d(plotwaterfall3d), d_plottime(plottime),
65     d_plotconst(plotconst),
66     d_parent(parent)
67 {
68   d_main_gui = NULL;
69   pthread_mutex_init(&d_pmutex, NULL);
70   lock();
71
72   // Perform fftshift operation;
73   // this is usually desired when plotting
74   d_shift = true;
75
76   d_fft = new gri_fft_complex (d_fftsize, true);
77
78   d_fftdata = new gr_complex[d_fftsize];
79
80   d_index = 0;
81   d_residbuf = new float[d_fftsize];
82
83   buildwindow();
84
85   initialize();
86 }
87
88 qtgui_sink_f::~qtgui_sink_f()
89 {
90   delete d_object;
91   delete [] d_fftdata;
92   delete [] d_residbuf;
93   delete d_fft;
94 }
95
96 void qtgui_sink_f::lock()
97 {
98   pthread_mutex_lock(&d_pmutex);
99 }
100
101 void qtgui_sink_f::unlock()
102 {
103   pthread_mutex_unlock(&d_pmutex);
104 }
105
106 void
107 qtgui_sink_f::initialize()
108 {
109   if(qApp != NULL) {
110     d_qApplication = qApp;
111   }
112   else {
113     int argc;
114     char **argv = NULL;
115     d_qApplication = new QApplication(argc, argv);
116   }
117
118
119   uint64_t maxBufferSize = 32768;
120   d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize,
121                                     d_fmin, d_fmax);
122   d_main_gui->SetDisplayTitle(d_name);
123   d_main_gui->SetFFTSize(d_fftsize);
124   d_main_gui->SetWindowType((int)d_wintype);
125
126   d_main_gui->OpenSpectrumWindow(d_parent,
127                                  d_plotfreq, d_plotwaterfall,
128                                  d_plotwaterfall3d, d_plottime,
129                                  d_plotconst);
130
131   d_object = new qtgui_obj(d_qApplication);
132   qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
133 }
134
135 void
136 qtgui_sink_f::exec_()
137 {
138   d_qApplication->exec();
139 }
140
141 QWidget*
142 qtgui_sink_f::qwidget()
143 {
144   return d_main_gui->qwidget();
145 }
146
147 PyObject*
148 qtgui_sink_f::pyqwidget()
149 {
150   PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
151   PyObject *retarg = Py_BuildValue("N", w);
152   return retarg;
153 }
154
155 void
156 qtgui_sink_f::set_frequency_range(const double centerfreq, 
157                                   const double startfreq,
158                                   const double stopfreq)
159 {
160   d_main_gui->SetFrequencyRange(centerfreq, startfreq, stopfreq);
161 }
162
163 void
164 qtgui_sink_f::fft(const float *data_in, int size, gr_complex *data_out)
165 {
166   if (d_window.size()) {
167     gr_complex *dst = d_fft->get_inbuf();
168     for (int i = 0; i < size; i++)              // apply window
169       dst[i] = data_in[i] * d_window[i];
170   }
171   else {
172       gr_complex *dst = d_fft->get_inbuf();
173       for (int i = 0; i < size; i++)            // float to complex conversion
174         dst[i] = data_in[i];
175   }
176   
177   d_fft->execute ();     // compute the fft
178
179   for(int i=0; i < size; i++) {
180     d_fft->get_outbuf()[i] /= size;
181   }
182
183   // copy result to our output
184   if(d_shift) {  // apply a fft shift on the data
185     unsigned int len = (unsigned int)(ceil(size/2.0));
186     memcpy(&data_out[0], &d_fft->get_outbuf()[len], sizeof(gr_complex)*(size - len));
187     memcpy(&data_out[size - len], &d_fft->get_outbuf()[0], sizeof(gr_complex)*len);
188   }
189   else {
190     memcpy(data_out, d_fft->get_outbuf(), sizeof(gr_complex)*size);
191   }
192 }
193
194 void 
195 qtgui_sink_f::windowreset()
196 {
197   gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();  
198   if(d_wintype != newwintype) {
199     d_wintype = newwintype;
200     buildwindow();
201   }
202 }
203
204 void
205 qtgui_sink_f::buildwindow()
206 {
207   d_window.clear();
208   if(d_wintype != 0) {
209     d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
210   }
211 }
212
213 void
214 qtgui_sink_f::fftresize()
215 {
216   int newfftsize = d_main_gui->GetFFTSize();
217
218   if(newfftsize != d_fftsize) {
219
220     // Resize the fftdata buffer; no need to preserve old data
221     delete [] d_fftdata;
222     d_fftdata = new gr_complex[newfftsize];
223
224     // Resize residbuf and replace data
225     delete [] d_residbuf;
226     d_residbuf = new float[newfftsize];
227
228     // Set new fft size and reset buffer index 
229     // (throws away any currently held data, but who cares?) 
230     d_fftsize = newfftsize;
231     d_index = 0;
232     
233     // Reset window to reflect new size
234     buildwindow();
235
236     // Reset FFTW plan for new size
237     delete d_fft;
238     d_fft = new gri_fft_complex (d_fftsize, true);
239   }
240 }
241
242
243 int
244 qtgui_sink_f::general_work (int noutput_items,
245                             gr_vector_int &ninput_items,
246                             gr_vector_const_void_star &input_items,
247                             gr_vector_void_star &output_items)
248 {
249   int i=0, j=0;
250   const float *in = (const float*)input_items[0];
251
252   pthread_mutex_lock(&d_pmutex);
253
254   if(d_index) {
255     int filler = std::min(d_fftsize - d_index, noutput_items);
256     memcpy(&d_residbuf[d_index], &in[0], sizeof(float)*filler);
257     d_index += filler;
258     i = filler;
259     j = filler;
260   }
261
262   if(d_index == d_fftsize) {
263     d_index = 0;
264     fft(d_residbuf, d_fftsize, d_fftdata);
265     
266     d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize,
267                              d_residbuf, d_fftsize, NULL, 0,
268                              1.0/4.0, convert_to_timespec(0.0), true);
269   }
270   
271   for(; i < noutput_items; i+=d_fftsize) {
272     if(noutput_items - i > d_fftsize) {
273       j += d_fftsize;
274       fft(&in[i], d_fftsize, d_fftdata);
275       
276       d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize, &in[i],
277                                d_fftsize, NULL, 0, 1.0/4.0,
278                                convert_to_timespec(0.0), true);
279     }
280   }
281
282   if(noutput_items > j) {
283     d_index = noutput_items - j;
284     memcpy(d_residbuf, &in[j], sizeof(float)*d_index);
285   }
286
287   pthread_mutex_unlock(&d_pmutex);
288
289   consume_each(noutput_items);
290   return noutput_items;
291 }