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_f.h>
28 #include <gr_io_signature.h>
34 qtgui_make_sink_f (int fftsize, const std::vector<float> &window,
35 float fmin, float fmax, const std::string &name)
37 return qtgui_sink_f_sptr (new qtgui_sink_f (fftsize, window, fmin, fmax, name));
40 qtgui_sink_f::qtgui_sink_f (int fftsize, const std::vector<float> &window,
41 float fmin, float fmax, const std::string &name)
43 gr_make_io_signature (1, 1, sizeof(float)),
44 gr_make_io_signature (0, 0, 0)),
45 d_fftsize(fftsize), d_window(window),
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 fftdata = new gr_complex[d_fftsize];
59 d_residbuf = new float[d_fftsize];
62 qtgui_sink_f::~qtgui_sink_f()
70 void qtgui_sink_f::lock()
72 pthread_mutex_lock(&d_pmutex);
75 void qtgui_sink_f::unlock()
77 pthread_mutex_unlock(&d_pmutex);
81 qtgui_sink_f::start_app()
83 d_qApplication = new QApplication(0, NULL);
85 uint64_t maxBufferSize = 32768;
86 d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize, d_fmin, d_fmax);
87 d_main_gui->SetDisplayTitle(d_name);
88 d_main_gui->OpenSpectrumWindow(NULL);
90 qtgui_obj object(d_qApplication);
91 qApp->postEvent(&object, new qtgui_event(&d_pmutex));
93 d_qApplication->exec();
98 qtgui_sink_f::fft(const float *data_in, int size, gr_complex *data_out)
100 if (d_window.size()) {
101 gr_complex *dst = d_fft->get_inbuf();
102 for (int i = 0; i < size; i++) // apply window
103 dst[i] = data_in[i] * d_window[i];
106 gr_complex *dst = d_fft->get_inbuf();
107 for (unsigned int i = 0; i < size; i++) // float to complex conversion
111 d_fft->execute (); // compute the fft
113 for(int i=0; i < size; i++) {
114 d_fft->get_outbuf()[i] /= size;
117 // copy result to our output
118 if(d_shift) { // apply a fft shift on the data
119 unsigned int len = (unsigned int)(ceil(size/2.0));
120 memcpy(&data_out[0], &d_fft->get_outbuf()[len], sizeof(gr_complex)*(size - len));
121 memcpy(&data_out[size - len], &d_fft->get_outbuf()[0], sizeof(gr_complex)*len);
124 memcpy(data_out, d_fft->get_outbuf(), sizeof(gr_complex)*size);
130 qtgui_sink_f::general_work (int noutput_items,
131 gr_vector_int &ninput_items,
132 gr_vector_const_void_star &input_items,
133 gr_vector_void_star &output_items)
136 const float *in = (const float*)input_items[0];
138 pthread_mutex_lock(&d_pmutex);
141 int filler = std::min(d_fftsize - d_index, noutput_items);
142 memcpy(&d_residbuf[d_index], &in[0], sizeof(float)*filler);
148 if(d_index == d_fftsize) {
150 fft(d_residbuf, d_fftsize, fftdata);
152 d_main_gui->UpdateWindow(true, fftdata, d_fftsize, d_residbuf, d_fftsize, NULL, 0,
153 1.0/4.0, convert_to_timespec(0.0), true);
156 for(; i < noutput_items; i+=d_fftsize) {
157 if(noutput_items - i > d_fftsize) {
159 fft(&in[i], d_fftsize, fftdata);
161 d_main_gui->UpdateWindow(true, fftdata, d_fftsize, &in[i], d_fftsize, NULL, 0,
162 1.0/4.0, convert_to_timespec(0.0), true);
166 if(noutput_items > j) {
167 d_index = noutput_items - j;
168 memcpy(d_residbuf, &in[j], sizeof(float)*d_index);
171 pthread_mutex_unlock(&d_pmutex);
173 consume_each(noutput_items);
174 return noutput_items;