Search code examples
audiosignal-processingfftfftwaudio-processing

Transforming Audio Samples From Time Domain to Frequency Domain


as a software engineer I am facing with some difficulties while working on a signal processing problem. I don't have much experience in this area.

What I try to do is to sample the environmental sound with 44100 sampling rate and for fixed size windows to test if a specific frequency (20KHz) exists and is higher than a threshold value.

Here is what I do according to the perfect answer in How to extract frequency information from samples from PortAudio using FFTW in C

102400 samples (2320 ms) is gathered from audio port with 44100 sampling rate. Sample values are between 0.0 and 1.0

int samplingRate = 44100;
int numberOfSamples = 102400;
float samples[numberOfSamples] = ListenMic_Function(numberOfSamples,samplingRate);

Window size or FFT Size is 1024 samples (23.2 ms)

int N = 1024;

Number of windows is 100

int noOfWindows = numberOfSamples / N;

Splitting samples to noOfWindows (100) windows each having size of N (1024) samples

float windowSamplesIn[noOfWindows][N];
for i:= 0 to noOfWindows -1 
    windowSamplesIn[i] = subarray(samples,i*N,(i+1)*N);
endfor

Applying Hanning window function on each window

float windowSamplesOut[noOfWindows][N];
for i:= 0 to noOfWindows -1 
    windowSamplesOut[i] = HanningWindow_Function(windowSamplesIn[i]);
endfor

Applying FFT on each window (real to complex conversion done inside the FFT function)

float frequencyData[noOfWindows][samplingRate/2]; 
for i:= 0 to noOfWindows -1 
    frequencyData[i] = RealToComplex_FFT_Function(windowSamplesOut[i], samplingRate);
endfor

In the last step, I use the FFT function implemented in this link: http://www.codeproject.com/Articles/9388/How-to-implement-the-FFT-algorithm ; because I cannot implement an FFT function from the scratch.

What I can't be sure is while giving N (1024) samples to FFT function as input, samplingRate/2 (22050) decibel values is returned as output. Is it what an FFT function does?

I understand that because of Nyquist Frequency, I can detect half of sampling rate frequency at most. But is it possible to get decibel values for each frequency up to samplingRate/2 (22050) Hz?

Thanks, Vahit


Solution

  • See see How do I obtain the frequencies of each value in an FFT?

    From a 1024 sample input, you can get back 512 meaningful frequency-levels.

    So, yes, within your window, you'll get back a level for the Nyquist frequency.

    The lowest frequency level you'll see is for DC (0 Hz), and the next one up will be for SampleRate/1024, or around 44 Hz, the next for 2 * SampleRate/1024, and so on, up to 512 * SampleRate / 1024 Hz.