Udhay wrote:
How to get the frequency of an audio file and how to separate the low
and high frequency of an audio file
Look at "Discrete Fourier Transform" and the implementations of a "Fast
Fourier Transform".
Here is some code I toyed with - I lifted it from somewhere, I don't
know exactly where and then I templateized it a little.
The FFT is done in the complex number space.... You could also look up
the DCT (discrete cosine transform) which I think operates in the real
number space but it's not as accurate - I really don't know much about
DCT's.
#define PI 3.1415926535897932384626
inline long revbin_update(long r,long n)
{
do {
n=n>>1;
r=r^n;
} while ((r&n)==0);
return r;
}
template <typename TranType>
inline void revbin_permute(TranType *a,long n)
{
if (n<=2) return;
long r=0;
for (long x=1; x<n; x++)
{
r=revbin_update(r,n);
if (r>x) swap(a[x],a[r]);
}
}
template <typename TranType>
TranType omega(double theta)
{ return TranType(cos(theta),sin(theta)); }
template <int is, typename TranType>
void fft(TranType *a, long ldn)
// O(N log N)
//
// a[] is the TranType input data set, lsb in 0 and msb in N
// ldn is the power of 2 which contains the entire data set, try to
// align the data to a power of 2 by padding with zeroes.
// is is the direction of the transform, +1 = forward, -1 = backward
//
// result is a[]
{
long n=1 << ldn;
revbin_permute(a,n);
for (long ldm=1; ldm<=ldn; ldm++)
{
long m=1 << ldm;
long mh=m/2;
for (long j=0; j<mh; j++)
{
TranType e=omega<TranType>(is*2*PI*j/m);
for (long r=0; r<=n-m; r+=m)
{
TranType u=a[r+j];
TranType v=a[r+j+mh]*e;
a[r+j]=(u+v);
a[r+j+mh]=(u-v);
}
}
}
}
template <typename TranType>
void fft_convolution(TranType *x,TranType *y, long n)
// x[], y[] are the two input TranType data sets.
// n is the power of 2 which contains the entire data set.
//
// result is y[]
{
long pw=1 << n;
fft<1>(x,n); // forward transform of x[]
fft<1>(y,n); // forward transfomr of y[]
for (int i=0; i<pw; i++)
y[i]=y[i]*x[i]; // element wise multiplication
fft<-1>(y,n); // backward transform of y[]
for (int i=0; i<pw; i++)
y[i]=y[i]/TranType(pw,0); // normalize
}
#include <complex>
/// example of use
int main()
{
static std::complex<double a[1<<21];
// revbin_permute( a, 1<<21 );
fft<1>( a, 21 );
}