1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-17 13:51:47 -05:00

Reduce FFT filter memory use.

This commit is contained in:
John Greb 2014-12-25 21:24:03 +00:00
parent 64de524abc
commit 3b8add19c3
2 changed files with 39 additions and 55 deletions

View File

@ -18,10 +18,8 @@ protected:
int flen2; int flen2;
g_fft<float> *fft; g_fft<float> *fft;
g_fft<float> *ift; g_fft<float> *ift;
cmplx *ht;
cmplx *filter; cmplx *filter;
cmplx *timedata; cmplx *data;
cmplx *freqdata;
cmplx *ovlbuf; cmplx *ovlbuf;
cmplx *output; cmplx *output;
int inptr; int inptr;

View File

@ -54,18 +54,14 @@ void fftfilt::init_filter()
fft = new g_fft<float>(flen); fft = new g_fft<float>(flen);
filter = new cmplx[flen]; filter = new cmplx[flen];
timedata = new cmplx[flen]; data = new cmplx[flen];
freqdata = new cmplx[flen]; output = new cmplx[flen2];
output = new cmplx[flen];
ovlbuf = new cmplx[flen2]; ovlbuf = new cmplx[flen2];
ht = new cmplx[flen];
memset(filter, 0, flen * sizeof(cmplx)); memset(filter, 0, flen * sizeof(cmplx));
memset(timedata, 0, flen * sizeof(cmplx)); memset(data, 0, flen * sizeof(cmplx));
memset(freqdata, 0, flen * sizeof(cmplx)); memset(output, 0, flen2 * sizeof(cmplx));
memset(output, 0, flen * sizeof(cmplx));
memset(ovlbuf, 0, flen2 * sizeof(cmplx)); memset(ovlbuf, 0, flen2 * sizeof(cmplx));
memset(ht, 0, flen * sizeof(cmplx));
inptr = 0; inptr = 0;
} }
@ -89,49 +85,40 @@ fftfilt::~fftfilt()
if (fft) delete fft; if (fft) delete fft;
if (filter) delete [] filter; if (filter) delete [] filter;
if (timedata) delete [] timedata; if (data) delete [] data;
if (freqdata) delete [] freqdata;
if (output) delete [] output; if (output) delete [] output;
if (ovlbuf) delete [] ovlbuf; if (ovlbuf) delete [] ovlbuf;
if (ht) delete [] ht;
} }
void fftfilt::create_filter(float f1, float f2) void fftfilt::create_filter(float f1, float f2)
{ {
// initialize the filter to zero // initialize the filter to zero
memset(ht, 0, flen * sizeof(cmplx)); memset(filter, 0, flen * sizeof(cmplx));
// create the filter shape coefficients by fft // create the filter shape coefficients by fft
// filter values initialized to the ht response h(t) bool b_lowpass, b_highpass;
bool b_lowpass, b_highpass;//, window;
b_lowpass = (f2 != 0); b_lowpass = (f2 != 0);
b_highpass = (f1 != 0); b_highpass = (f1 != 0);
for (int i = 0; i < flen2; i++) { for (int i = 0; i < flen2; i++) {
ht[i] = 0; filter[i] = 0;
//combine lowpass / highpass // lowpass @ f2
// lowpass @ f2 if (b_lowpass)
if (b_lowpass) ht[i] += fsinc(f2, i, flen2); filter[i] += fsinc(f2, i, flen2);
// highighpass @ f1 // highighpass @ f1
if (b_highpass) ht[i] -= fsinc(f1, i, flen2); if (b_highpass)
filter[i] -= fsinc(f1, i, flen2);
} }
// highpass is delta[flen2/2] - h(t) // highpass is delta[flen2/2] - h(t)
if (b_highpass && f2 < f1) ht[flen2 / 2] += 1; if (b_highpass && f2 < f1)
filter[flen2 / 2] += 1;
for (int i = 0; i < flen2; i++) for (int i = 0; i < flen2; i++)
ht[i] *= _blackman(i, flen2); filter[i] *= _blackman(i, flen2);
// this may change since green fft is in place fft
memcpy(filter, ht, flen * sizeof(cmplx));
// ht is flen complex points with imaginary all zero
// first half describes h(t), second half all zeros
// perform the cmplx forward fft to obtain H(w)
// filter is flen/2 complex values
fft->ComplexFFT(filter); fft->ComplexFFT(filter);
// normalize the output filter for unity gain // normalize the output filter for unity gain
float scale = 0, mag; float scale = 0, mag;
for (int i = 0; i < flen2; i++) { for (int i = 0; i < flen2; i++) {
mag = abs(filter[i]); mag = abs(filter[i]);
@ -146,22 +133,22 @@ void fftfilt::create_filter(float f1, float f2)
// Filter with fast convolution (overlap-add algorithm). // Filter with fast convolution (overlap-add algorithm).
int fftfilt::runFilt(const cmplx & in, cmplx **out) int fftfilt::runFilt(const cmplx & in, cmplx **out)
{ {
timedata[inptr++] = in; data[inptr++] = in;
if (inptr < flen2) if (inptr < flen2)
return 0; return 0;
inptr = 0; inptr = 0;
memcpy(freqdata, timedata, flen * sizeof(cmplx)); fft->ComplexFFT(data);
fft->ComplexFFT(freqdata);
for (int i = 0; i < flen; i++) for (int i = 0; i < flen; i++)
freqdata[i] *= filter[i]; data[i] *= filter[i];
fft->InverseComplexFFT(freqdata); fft->InverseComplexFFT(data);
for (int i = 0; i < flen2; i++) { for (int i = 0; i < flen2; i++) {
output[i] = ovlbuf[i] + freqdata[i]; output[i] = ovlbuf[i] + data[i];
ovlbuf[i] = freqdata[flen2 + i]; ovlbuf[i] = data[flen2 + i];
} }
memset (data, 0, flen * sizeof(cmplx));
*out = output; *out = output;
return flen2; return flen2;
@ -170,35 +157,34 @@ int fftfilt::runFilt(const cmplx & in, cmplx **out)
// Second version for single sideband // Second version for single sideband
int fftfilt::runSSB(const cmplx & in, cmplx **out, bool usb) int fftfilt::runSSB(const cmplx & in, cmplx **out, bool usb)
{ {
timedata[inptr++] = in; data[inptr++] = in;
if (inptr < flen2) if (inptr < flen2)
return 0; return 0;
inptr = 0; inptr = 0;
memcpy(freqdata, timedata, flen * sizeof(cmplx)); fft->ComplexFFT(data);
fft->ComplexFFT(freqdata);
// Discard frequencies for ssb // Discard frequencies for ssb
if ( usb ) if ( usb )
for (int i = 0; i < flen2; i++) { for (int i = 0; i < flen2; i++) {
freqdata[i] *= filter[i]; data[i] *= filter[i];
freqdata[flen2 + i] = 0; data[flen2 + i] = 0;
} }
else else
for (int i = 0; i < flen2; i++) { for (int i = 0; i < flen2; i++) {
freqdata[i] = 0; data[i] = 0;
freqdata[flen2 + i] *= filter[flen2 + i]; data[flen2 + i] *= filter[flen2 + i];
} }
// in-place FFT: freqdata overwritten with filtered timedata // in-place FFT: freqdata overwritten with filtered timedata
fft->InverseComplexFFT(freqdata); fft->InverseComplexFFT(data);
// overlap and add // overlap and add
for (int i = 0; i < flen2; i++) { for (int i = 0; i < flen2; i++) {
output[i] = ovlbuf[i] + freqdata[i]; output[i] = ovlbuf[i] + data[i];
ovlbuf[i] = freqdata[i+flen2]; ovlbuf[i] = data[i+flen2];
} }
memset (data, 0, flen * sizeof(cmplx));
*out = output; *out = output;
return flen2; return flen2;