3 * Copyright 2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
27 #include <qtgui_sink_c.h>
28 #include <gr_io_signature.h>
34 qtgui_make_sink_c (int fftsize, int wintype,
35 float fmin, float fmax, const std::string &name)
37 return qtgui_sink_c_sptr (new qtgui_sink_c (fftsize, wintype, fmin, fmax, name));
40 qtgui_sink_c::qtgui_sink_c (int fftsize, int wintype,
41 float fmin, float fmax, const std::string &name)
43 gr_make_io_signature (1, 1, sizeof(gr_complex)),
44 gr_make_io_signature (0, 0, 0)),
45 d_fftsize(fftsize), d_wintype((gr_firdes::win_type)(wintype)),
46 d_fmin(fmin), d_fmax(fmax), d_name(name)
49 pthread_mutex_init(&d_pmutex, NULL);
52 d_shift = true; // Perform fftshift operation; this is usually desired when plotting
54 d_fft = new gri_fft_complex (d_fftsize, true);
56 d_fftdata = new gr_complex[d_fftsize];
59 d_residbuf = new gr_complex[d_fftsize];
64 qtgui_sink_c::~qtgui_sink_c()
72 void qtgui_sink_c::lock()
74 pthread_mutex_lock(&d_pmutex);
77 void qtgui_sink_c::unlock()
79 pthread_mutex_unlock(&d_pmutex);
83 qtgui_sink_c::start_app()
87 d_qApplication = new QApplication(argc, argv);
89 uint64_t maxBufferSize = 32768;
90 d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize, d_fmin, d_fmax);
91 d_main_gui->SetDisplayTitle(d_name);
92 d_main_gui->SetFFTSize(d_fftsize);
93 d_main_gui->SetWindowType((int)d_wintype);
94 d_main_gui->OpenSpectrumWindow(NULL);
96 qtgui_obj object(d_qApplication);
97 qApp->postEvent(&object, new qtgui_event(&d_pmutex));
99 d_qApplication->exec();
104 qtgui_sink_c::fft(const gr_complex *data_in, int size, gr_complex *data_out)
106 if (d_window.size()) {
107 gr_complex *dst = d_fft->get_inbuf();
109 for (i = 0; i < size; i++) // apply window
110 dst[i] = data_in[i] * d_window[i];
113 memcpy (d_fft->get_inbuf(), data_in, sizeof(gr_complex)*size);
116 d_fft->execute (); // compute the fft
118 for(int i=0; i < size; i++) {
119 d_fft->get_outbuf()[i] /= size;
122 // copy result to our output
123 if(d_shift) { // apply a fft shift on the data
124 unsigned int len = (unsigned int)(ceil(size/2.0));
125 memcpy(&data_out[0], &d_fft->get_outbuf()[len], sizeof(gr_complex)*(size - len));
126 memcpy(&data_out[size - len], &d_fft->get_outbuf()[0], sizeof(gr_complex)*len);
129 memcpy(data_out, d_fft->get_outbuf(), sizeof(gr_complex)*size);
134 qtgui_sink_c::windowreset()
136 gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();
137 if(d_wintype != newwintype) {
138 d_wintype = newwintype;
144 qtgui_sink_c::buildwindow()
148 d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
153 qtgui_sink_c::fftresize()
155 int newfftsize = d_main_gui->GetFFTSize();
157 if(newfftsize != d_fftsize) {
159 // Resize the fftdata buffer; no need to preserve old data
161 d_fftdata = new gr_complex[newfftsize];
163 // Resize residbuf and replace data
164 delete [] d_residbuf;
165 d_residbuf = new gr_complex[newfftsize];
167 // Set new fft size and reset buffer index
168 // (throws away any currently held data, but who cares?)
169 d_fftsize = newfftsize;
172 // Reset window to reflect new size
175 // Reset FFTW plan for new size
177 d_fft = new gri_fft_complex (d_fftsize, true);
183 qtgui_sink_c::general_work (int noutput_items,
184 gr_vector_int &ninput_items,
185 gr_vector_const_void_star &input_items,
186 gr_vector_void_star &output_items)
189 const gr_complex *in = (const gr_complex*)input_items[0];
191 pthread_mutex_lock(&d_pmutex);
193 // Update the FFT size from the application
198 int filler = std::min(d_fftsize - d_index, noutput_items);
200 memcpy(&d_residbuf[d_index], &in[0], sizeof(gr_complex)*filler);
206 if(d_index == d_fftsize) {
208 fft(d_residbuf, d_fftsize, d_fftdata);
210 d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize, NULL, 0, (float*)d_residbuf, d_fftsize,
211 1.0/4.0, convert_to_timespec(0.0), true);
214 for(; i < noutput_items; i+=d_fftsize) {
215 if(noutput_items - i > d_fftsize) {
217 fft(&in[i], d_fftsize, d_fftdata);
219 d_main_gui->UpdateWindow(true, d_fftdata, d_fftsize, NULL, 0, (float*)&in[i], d_fftsize,
220 1.0/4.0, convert_to_timespec(0.0), true);
224 if(noutput_items > j) {
225 d_index = noutput_items - j;
226 memcpy(d_residbuf, &in[j], sizeof(gr_complex)*d_index);
229 pthread_mutex_unlock(&d_pmutex);
231 consume_each(noutput_items);
232 return noutput_items;