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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/* ------------------------------------------------------------------
libofa -- the Open Fingerprint Architecture library
Copyright (C) 2006 MusicIP Corporation
All rights reserved.
-------------------------------------------------------------------*/
// FILE: "fft_op.cpp"
// MODULE: Implementation for class FFT_op
// AUTHOR: Frode Holm
// DATE CREATED: 1/12/06
#include <vector>
#include <math.h>
#include "ofa1/ofa.h"
#include "fft_op.h"
#include "error_op.h"
#define ROUND(x) ((x>0)? (long)floor(x + 0.5) : (long)(ceil(x - 0.5)) )
FFT_op::FFT_op()
{
FrameSize = 0;
NumBins = 0;
NumFrames = 0;
TimeSpectra = 0;
BufSize = 0;
OutBuf = 0;
InBuf = 0;
AmpSpectWin = 0;
Hamming = 0;
Overlap = 0;
Rate = 0;
}
FFT_op::~FFT_op()
{
FFTLib_op::Destroy();
if (OutBuf)
delete[] OutBuf;
if (InBuf)
delete[] InBuf;
if (AmpSpectWin)
delete[] AmpSpectWin;
if (TimeSpectra)
delete[] TimeSpectra;
if (Hamming)
delete[] Hamming;
}
void
FFT_op::LoadSignal(Signal_op *sig)
{
Signal = sig;
Rate = Signal->GetRate();
if (TimeSpectra)
{
delete[] TimeSpectra;
TimeSpectra = 0;
}
}
void
FFT_op::SetSize(int N, bool optimize)
{
if (OutBuf)
delete[] OutBuf;
if (InBuf)
delete[] InBuf;
if (AmpSpectWin)
delete[] AmpSpectWin;
FrameSize = N;
OutBuf = new double[FrameSize+128];
InBuf = new double[FrameSize+128];
FFTLib_op::SetSize(N, optimize, InBuf, OutBuf);
SetNumBins(FrameSize/2 + 1);
AmpSpectWin = new double[GetNumBins()];
WindowInit();
}
void
FFT_op::SetStep(int step) {
if (Rate==0)
throw OnePrintError("SetStep:programming error:Rate");
if (step<=0)
throw OnePrintError("SetStep:programming error:Step");
StepSize = step;
}
void
FFT_op::WindowInit()
{
if (Hamming)
delete[] Hamming;
Hamming = new double[FrameSize];
for (int i=0; i<FrameSize; i++)
Hamming[i] = 0.54 - 0.46*cos(i*(TwoPI/(FrameSize-1)));
}
void
FFT_op::CreateBuffer(int numBins, int numFrames, bool init)
{
NumFrames = numFrames;
NumBins = numBins;
BufSize = NumFrames * NumBins;
if (TimeSpectra) delete[] TimeSpectra;
TimeSpectra = new float[BufSize];
if (init)
{
for (int i=0; i<BufSize; i++)
TimeSpectra[i] = 0;
}
}
// Mono signals only
void
FFT_op::Compute(double ovlap)
{
long i;
int j,k,m;
if (ovlap != Overlap || !TimeSpectra)
{
Overlap = ovlap;
if (TimeSpectra)
delete[] TimeSpectra;
SetStep(int(FrameSize * (1.0 - Overlap))); // # of signal samples per step
SetNumFrames(((Signal->GetLength()-FrameSize) / StepSize) + 1);
CreateBuffer(GetNumBins(), GetNumFrames()); // allocates spectrum storage
}
short* sdata = Signal->GetBuffer();
j = BufSize; // safety
// m counts # of StepSize's we've made
for (i=0, m=0; i<=Signal->GetLength()-FrameSize; i+=StepSize, m++)
{
for (j=0; j<FrameSize; j++) {
// copy and normalize samples into fft input buffer
InBuf[j] = (double)sdata[i+j]/(double)MaxSample;
}
// Do the FFT
ComputeWindow(InBuf);
// Copy resulting spectrum into the larger array
long start = m * GetNumBins();
for (j=start, k=0; k < GetNumBins(); j++, k++) {
TimeSpectra[j] = (float)AmpSpectWin[k];
}
}
// zero out remaining entries
for ( ; j<BufSize; j++)
TimeSpectra[j] = (float) 0.0;
}
// If windowing other than RECTANGULAR is in effect, the input buffer will be altered
void
FFT_op::ComputeWindow(double* in)
{
int i;
if (WindowShape == HAMMING)
{
for (i=0; i < FrameSize; i++)
in[i] *= Hamming[i];
}
FFTLib_op::ComputeFrame(FrameSize, in, OutBuf);
// Normalize
for (i=0; i < FrameSize; i++)
OutBuf[i] /= FrameSize;
// Compute amplitude spectrum for window
// We only got half the values, because the rest was thrown away in the (identical)
// complex conjugate part (negative frequencies). To get the amplitude back
// we must multiply by 2.
AmpSpectWin[0] = 2*sqrt(OutBuf[0]*OutBuf[0]); // DC component
for (int k=1; k<(FrameSize+1)/2; ++k) // (k < N/2 rounded up)
AmpSpectWin[k] = 2*sqrt(OutBuf[k]*OutBuf[k] + OutBuf[FrameSize-k]*OutBuf[FrameSize-k]);
if (FrameSize % 2 == 0) // N is even
AmpSpectWin[FrameSize/2] = 2*sqrt(OutBuf[FrameSize/2]*OutBuf[FrameSize/2]); // Nyquist freq.
}
// Resample the frames to a new reduced size
void
FFT_op::ReSample(int nBins, bool melScale)
{
double hiFreq = 8000.0; // Everything above 8 KHz is ignored
double halfFreq;
if (melScale)
halfFreq = 1000.0;
else
halfFreq = hiFreq/2;
if (GetFreqStep() > halfFreq/(nBins/2) || nBins>=GetNumBins())
throw OnePrintError("Oversampling not supported in ReSample");
int j;
float* fr;
int srcInd, curInd;
double fStep, maxAmp, curHz, srcHz;
// Pre-calculate frequencies
vector<double> freq(GetNumBins());
for (j=0; j<GetNumBins(); j++)
freq[j] = GetFreq(j);
float* tmpBuf = new float[nBins*GetNumFrames()];
// Approximate the Barks scale: 1/2 the bins from 0-halfFreq Hz, 1/2 from halfFreq-hiFreq Hz
for (long i=0; i<GetNumFrames(); i++)
{
fr = GetFrame(i);
curHz = 0;
srcInd = 0;
curInd = 0;
srcHz = freq[srcInd];
fStep = halfFreq/(nBins/2);
for (j=0; j<nBins/2; j++)
{
curHz += fStep;
maxAmp = 0;
while (srcHz < curHz)
{
if (fr[srcInd] > maxAmp) maxAmp = fr[srcInd];
srcInd++;
srcHz = freq[srcInd];
}
tmpBuf[i*nBins+j] = (float)maxAmp;
}
fStep = (hiFreq-halfFreq)/(nBins/2);
for (j=nBins/2; j<nBins; j++)
{
curHz += fStep;
maxAmp = 0;
while (srcHz < curHz)
{
if (fr[srcInd] > maxAmp) maxAmp = fr[srcInd];
srcInd++;
srcHz = freq[srcInd];
}
tmpBuf[i*nBins+j] = (float)maxAmp;
}
}
delete[] TimeSpectra;
TimeSpectra = tmpBuf;
SetNumBins(nBins);
BufSize = GetNumFrames() * GetNumBins();
}
// convert Hz to MIDI note number.
int
FFT_op::FreqToMidi(double hz)
{
const double nFact = 17.31234049067; // 12/ln(2)
double Nd;
Nd = nFact*log(hz/27.5);
return ROUND(Nd);
}
|