Mercurial > hg > svgui
comparison widgets/WindowShapePreview.cpp @ 1169:b5c88c448ca2 3.0-integration
Merge from branch bqfft
author | Chris Cannam |
---|---|
date | Mon, 21 Nov 2016 15:33:08 +0000 |
parents | 6796afa25c88 |
children | d39db4673676 |
comparison
equal
deleted
inserted
replaced
1167:236ecb2c0758 | 1169:b5c88c448ca2 |
---|---|
20 #include <QPainter> | 20 #include <QPainter> |
21 #include <QPainterPath> | 21 #include <QPainterPath> |
22 #include <QFont> | 22 #include <QFont> |
23 #include <QString> | 23 #include <QString> |
24 | 24 |
25 #include "data/fft/FFTapi.h" | 25 #include <bqfft/FFT.h> |
26 | 26 |
27 #include <vector> | |
28 #include <complex> | |
27 #include <iostream> | 29 #include <iostream> |
30 | |
31 using namespace std; | |
28 | 32 |
29 | 33 |
30 WindowShapePreview::WindowShapePreview(QWidget *parent) : | 34 WindowShapePreview::WindowShapePreview(QWidget *parent) : |
31 QFrame(parent), | 35 QFrame(parent), |
32 m_windowType(HanningWindow) | 36 m_windowType(HanningWindow) |
124 QPainter freqPainter(&freqLabel); | 128 QPainter freqPainter(&freqLabel); |
125 path = QPainterPath(); | 129 path = QPainterPath(); |
126 | 130 |
127 int fftsize = 512; | 131 int fftsize = 512; |
128 | 132 |
129 float *input = (float *)fftf_malloc(fftsize * sizeof(float)); | 133 breakfastquay::FFT fft(fftsize); |
130 fftf_complex *output = | 134 |
131 (fftf_complex *)fftf_malloc(fftsize * sizeof(fftf_complex)); | 135 vector<float> input(fftsize); |
132 fftf_plan plan = fftf_plan_dft_r2c_1d(fftsize, input, output, | 136 vector<complex<float>> output(fftsize/2 + 1); |
133 FFTW_ESTIMATE); | 137 |
134 for (int i = 0; i < fftsize; ++i) input[i] = 0.f; | 138 for (int i = 0; i < fftsize; ++i) input[i] = 0.f; |
135 for (int i = 0; i < step * 2; ++i) { | 139 for (int i = 0; i < step * 2; ++i) { |
136 input[fftsize/2 - step + i] = windower.getValue(i); | 140 input[fftsize/2 - step + i] = windower.getValue(i); |
137 } | 141 } |
138 | 142 |
139 fftf_execute(plan); | 143 fft.forwardInterleaved(input.data(), reinterpret_cast<float *>(output.data())); |
140 fftf_destroy_plan(plan); | |
141 | 144 |
142 float maxdb = 0.f; | 145 float maxdb = 0.f; |
143 float mindb = 0.f; | 146 float mindb = 0.f; |
144 bool first = true; | 147 bool first = true; |
145 for (int i = 0; i < fftsize/2; ++i) { | 148 for (int i = 0; i < fftsize/2; ++i) { |
146 float power = output[i][0] * output[i][0] + output[i][1] * output[i][1]; | 149 float power = |
150 output[i].real() * output[i].real() + | |
151 output[i].imag() * output[i].imag(); | |
147 float db = mindb; | 152 float db = mindb; |
148 if (power > 0) { | 153 if (power > 0) { |
149 db = 20.f * log10f(power); | 154 db = 20.f * log10f(power); |
150 if (first || db > maxdb) maxdb = db; | 155 if (first || db > maxdb) maxdb = db; |
151 if (first || db < mindb) mindb = db; | 156 if (first || db < mindb) mindb = db; |
174 freqPainter.setPen(Qt::black); | 179 freqPainter.setPen(Qt::black); |
175 | 180 |
176 // cerr << "maxdb = " << maxdb << ", mindb = " << mindb << ", maxval = " <<maxval << endl; | 181 // cerr << "maxdb = " << maxdb << ", mindb = " << mindb << ", maxval = " <<maxval << endl; |
177 | 182 |
178 for (int i = 0; i < fftsize/2; ++i) { | 183 for (int i = 0; i < fftsize/2; ++i) { |
179 float power = output[i][0] * output[i][0] + output[i][1] * output[i][1]; | 184 float power = |
185 output[i].real() * output[i].real() + | |
186 output[i].imag() * output[i].imag(); | |
180 float db = 20.f * log10f(power); | 187 float db = 20.f * log10f(power); |
181 float val = db + -mindb; | 188 float val = db + -mindb; |
182 if (val < 0) val = 0; | 189 if (val < 0) val = 0; |
183 float norm = val / maxval; | 190 float norm = val / maxval; |
184 float x = (float(w) / float(fftsize/2)) * float(i); | 191 float x = (float(w) / float(fftsize/2)) * float(i); |
189 | 196 |
190 freqPainter.setRenderHint(QPainter::Antialiasing, true); | 197 freqPainter.setRenderHint(QPainter::Antialiasing, true); |
191 path.addRect(0, 0, w, h + 1); | 198 path.addRect(0, 0, w, h + 1); |
192 freqPainter.drawPath(path); | 199 freqPainter.drawPath(path); |
193 | 200 |
194 fftf_free(input); | |
195 fftf_free(output); | |
196 | |
197 freqPainter.setFont(font); | 201 freqPainter.setFont(font); |
198 label = tr("dB / freq"); | 202 label = tr("dB / freq"); |
199 freqPainter.drawText(w - freqPainter.fontMetrics().width(label) - 4, | 203 freqPainter.drawText(w - freqPainter.fontMetrics().width(label) - 4, |
200 freqPainter.fontMetrics().ascent() + 1, label); | 204 freqPainter.fontMetrics().ascent() + 1, label); |
201 | 205 |