Reputation: 11
I'm doing some sound analysis with my microphone in the smartphone. Therefore i am recording the sound and do a FFT on it. After that I am printing my values.
I use a low-pass filter to cut off everything over 200hz.
for (int i = 0; i < blockSize; i++){
// lowpass filter before analyzing, possibility to cut frequencies above
int cutFreqFilterLowPass = 200 ; // lowpass frequency
int cutFreqBandPass=600;
for (int ii = 0; ii < audioDataDoubles.length; ii += 2) {
// if (ii < (((cutFreqBandPass - 100) * (audioDataDoubles.length/2)) / SAMPLING_RATE)*2 ||
ii > (((cutFreqBandPass + 100) * (audioDataDoubles.length/2)) / SAMPLING_RATE)*2){
if (ii > ((cutFreqFilterLowPass * (audioDataDoubles.length/2)) / SAMPLING_RATE)*2){
audioDataDoubles[ii] = audioDataDoubles[ii + 1] = 0.0;}
// audioDataDoubles_copy[ii]=audioDataDoubles[ii];
// System.out.println("gecutteter Frequenzbereich "+audioDataDoubles[ii]);
}
}
But now, when I am whistling I get values in the first bins ... why is that?
for (int i = 0; i < blockSize; i++) {
// real is stored in first part of array
re[i] = audioDataDoubles[i * 2];
// imaginary is stored in the sequential part
im[i] = audioDataDoubles[(i * 2) + 1];
// magnitude is calculated by the square root of (imaginary^2 + real^2)
magnitude[i] = Math.sqrt((re[i] * re[i]) + (im[i] * im[i]));
}
I'm curious about having values about 1-2. Something in the first 2 bins at about 40-80 and 80-120 hz.
I used to zero out the first bin? Is this useful?
Is this normal or is my FFT not working right?
Seems to make no sense why I'm getting values there..
Upvotes: 1
Views: 218
Reputation: 70733
When recording live audio, there is usually an ambient noise floor (room noise and/or thermal noise floor). This noise is often wide-spectrum and thus can appear in all FFT bins. You can record your noise level during quiet times, compute the statistics of this noise, and only consider something in an FFT result bin to be valid if it is above some statistical threshold (3 sigma or some such).
Any impulse, whether the click at the beginning of a whistle or a bass drum is also wide-spectrum, and thus can also appear in all bins.
A windowing transient can also produced by the rectangular windowing involved in feeding live audio to any finite length FFT, and this can also produce some noise in all bins for any frequency content not exactly integer periodic in the FFT width. A bell shaped window (von Hann, et.al.) will help reduce this artifact. A modulated DC offset in the FFT input can also produce low frequency noise in an FFT result. Computing and subtracting any DC offset can help reduce this artifact as well.
Upvotes: 0
Reputation: 40366
Two things:
There could actually be low frequency noise in your signal, if it is a low quality microphone or audio interface, or there is high interference (e.g. ground hum [although probably not for a mobile device], low quality PCB design, nearby sources of interference). What you are seeing very much could be a fairly accurate representation of your signal, however:
FFTs will sometimes include other frequency components that are related to the difference in the endpoints of the source signal sample. An FFT essentially assumes your input block is a loop, and if it does not loop cleanly you may expect to see other components. For example, if you take an arbitrary block in the middle of a pure sine wave signal somewhere, the phase offset may require that other frequency components be present in the FFT in order to precisely represent the out-of-phase signal.
To combat #2, the usual approach is the use of window functions, which are used to modify the input signal to reduce the effects of the sudden "boundaries" at the edges of the block. At its most basic, a window function could be a simple linear fade in at the start of your block and fade out at the end of the block (but this is a very crude function). The window function you choose will affect the frequency response of your FFT (often, frequency analysis applications will give the user a choice of windowing functions so that the most appropriate one for the given situation may be selected on-the-fly). Check out this page for a nice overview and write up.
A nice relevant blurb and graphic from that page:
The abrupt discontinuity at the record's end points produces frequency components not present in the original signal, which introduces spectral leakage in the FFT. This can be understood by looking into the case where the sampled signal is a sinusoid:
By the way, FFT-based filters can be tricky to implement, because some information is lost while windowing, and also overlap and cross-fading of blocks must be done to eliminate audible boundaries between blocks after removing FFT frequency components (which can change the phase and DC offset of the signal). Latency can also be an issue depending on the desired resolution of your FFT.
You may want to have a look at FIR filters if all you are trying to do is apply a fixed filter to audio. They are efficient and particularly easy to implement. There are plenty of simple FIR filter designer tools available, many of them free.
Upvotes: 1