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