-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFftBase.cpp
More file actions
154 lines (120 loc) · 3.93 KB
/
Copy pathFftBase.cpp
File metadata and controls
154 lines (120 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) 2010-2016 Stephane Poirier
*
* stephane.poirier@oifii.org
*
* Stephane Poirier
* 3532 rue Ste-Famille, #3
* Montreal, QC, H2X 2L1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "FftBase.h"
//Constructor
FftBase :: FftBase(long N_In, long M_In)
{
assert( M_In >= N_In );
assert( N_In > 0 );
N = N_In;
M = M_In;
tempBuffer = new complex[M];
//initialize lookup tables
sinLookupTable = new SAMPLE[M];
cosLookupTable = new SAMPLE[M];
makeTrigTables( sinLookupTable, cosLookupTable );
bitReverseLookupTable = new int[M];
makeBitReverseTable( bitReverseLookupTable );
}
FftBase :: ~FftBase()
{
delete []tempBuffer;
delete []sinLookupTable;
delete []cosLookupTable;
delete []bitReverseLookupTable;
}
void FftBase :: setN(long N_In)
{
assert( M >= N ); //Assert that the new input signal length is less than the total fft length
N = N_In;
}
void FftBase :: setM(long M_In )
{
assert( M_In >= N );
M = M_In;
delete []sinLookupTable;
delete []cosLookupTable;
delete []bitReverseLookupTable;
sinLookupTable = new SAMPLE[M];
cosLookupTable = new SAMPLE[M];
makeTrigTables( sinLookupTable, cosLookupTable );
bitReverseLookupTable = new int[M];
makeBitReverseTable( bitReverseLookupTable );
delete []tempBuffer;
tempBuffer = new complex[M];
}
void FftBase :: fft(complex* fftBuffer)
{
// performs an in-place fft on the complex data in the inBuffer
// bit reversing should already have been done.
for (int halfSize = 1; halfSize < M; halfSize *= 2)
{
SAMPLE phaseShiftStepR = cosLookupTable[halfSize];
SAMPLE phaseShiftStepI = sinLookupTable[halfSize];
// current phase shift
SAMPLE currentPhaseShiftR = 1.0;
SAMPLE currentPhaseShiftI = 0.0;
for (int fftStep = 0; fftStep < halfSize; fftStep++)
{
for (int i = fftStep; i < M; i += 2 * halfSize)
{
int offset = i + halfSize;
SAMPLE tr = (currentPhaseShiftR * fftBuffer[offset].real) - (currentPhaseShiftI * fftBuffer[offset].imag);
SAMPLE ti = (currentPhaseShiftR * fftBuffer[offset].imag) + (currentPhaseShiftI * fftBuffer[offset].real);
fftBuffer[offset].real = fftBuffer[i].real - tr;
fftBuffer[offset].imag = fftBuffer[i].imag - ti;
fftBuffer[i].real += tr;
fftBuffer[i].imag += ti;
}
SAMPLE tmpR = currentPhaseShiftR;
currentPhaseShiftR = (tmpR * phaseShiftStepR) - (currentPhaseShiftI * phaseShiftStepI);
currentPhaseShiftI = (tmpR * phaseShiftStepI) + (currentPhaseShiftI * phaseShiftStepR);
}
}
}
/*
void FftBase :: copyNRealInputToMLongTempBufferBitReversed(SAMPLE* inBuffer)
{
}
*/
//Builds trig fn lookup tables for size M Fft
void FftBase :: makeTrigTables(SAMPLE* inBufferSin, SAMPLE* inBufferCos)
{
//0 index not used, set to 0 to be safe
inBufferSin[0] = 0;
inBufferCos[0] = 0;
for( int i = 1; i < M; i++ )
{
inBufferSin[i] = sin( -MY_PI / i );
inBufferCos[i] = cos( -MY_PI / i );
}
}
//Builds a bit reverse lookup table for a length M Fft where bitReverseLookupTable[i] = (i, bit reversed)
void FftBase :: makeBitReverseTable(int* inBitReverseLookupTable)
{
inBitReverseLookupTable[0] = 0;
for (int limit = 1, bit = M / 2; limit < M; limit <<= 1, bit >>= 1)
for (int i = 0; i < limit; i++)
inBitReverseLookupTable[i + limit] = inBitReverseLookupTable[i] + bit;
}