I wrote a DSP program that uses overlap-add like this:
1. Incoming data is segmented into blocks of N samples
2. Each block is expanded with another N zeros, so the block has now 2N samples
3. Put the 2N block through FFT, getting a 2N spectrum
4. Multiply the 2N spectrum with a Sinc lowpass filter kernel spectrum
5. Put the resulting 2N spectrum through inverse FFT, getting a 2N block
6. Adding the first N samples to the last N samples of the preceeding run
This seems to work perfectly fine. Let's say I feed a nice sine signal to my input, so I get a nice (low pass filtered) sine at the output. I want to point out that the output signal does not show any indication that it was processed in blocks.
Now here is what puzzles me:
They say that you also need to use a windowing function. I take it that I am supposed put every input block (N samples) through a windowing function before I expand it to 2N samples.
But if I do so, the nice sine signal is faded in and faded out with every block. Of course, the output signal looks simular: what was once a nice sine signal is now an continuous repetition of fading in and fading out. This is not what I want, do I?
It seems to me that doing windowing is wrong when using overlap-add. As far as I understand windowing is useful when looking at the spectrum of an isolated block of data, but with overlap-add I have some sort of continuous stream of data, right?
When doing fast convolution via overlap-add, one does not normally (non-rectangularly) window the input data.
But you may need to window your filter kernel to make sure that it isn't too long. The sum of the data block length (N) plus the filter impulse response or kernel length (M) must not be longer than the FFT length (>= N+M-1)
If you want to do analysis in conjunction with filtering, then one possibility is to overlap the input data by 50% and use von Hann windows. That works because the sum of the magnitudes of 50% overlapped von Hann windows is an unmodulated constant (except the very first and very last block). So the data does not fade in and out after the summation (overlap-add). Or you can just use separate FFTs, a windowed FFT for analysis, and an un-windowed FFT for overlap fast convolution.
Related
I have time series data that I want to decompose into its intrinsic frequencies. After running FFT on the series, I am left with a periodogram with the main frequencies.
Is there any way to extract a specific frequency and convert it back into the original time-domain?
For example, if I find a 7-day frequency, I can understand that this is a weekly cycle, however, is there any way to understand on what day the peaks occur?
Use a complex FFT instead of a scalar periodogram. The magnitude of a complex FFT shows the peaks, but the complex inverse (complex IFFT) of a single complex FFT peak (might be more than one sample in width) will retain the phase information and result in the proper time relationship of a particular cyclic component to the original time series.
Note: The input to the complex IFFT needs to retain conjugate symmetry for the result to be real instead of complex (except for rounding/quantization noise).
I'm currently working on a program in C++ in which I am computing the time varying FFT of a wav file. I have a question regarding plotting the results of an FFT.
Say for example I have a 70 Hz signal that is produced by some instrument with certain harmonics. Even though I say this signal is 70 Hz, it's a real signal and I assume will have some randomness in which that 70Hz signal varies. Say I sample it for 1 second at a sample rate of 20kHz. I realize the sample period probably doesn't need to be 1 second, but bear with me.
Because I now have 20000 samples, when I compute the FFT. I will have 20000 or (19999) frequency bins. Let's also assume that my sample rate in conjunction some windowing techniques minimize spectral leakage.
My question then: Will the FFT still produce a relatively ideal impulse at 70Hz? Or will there 'appear to be' spectral leakage which is caused by the randomness the original signal? In otherwords, what does the FFT look like of a sinusoid whose frequency is a random variable?
Some of the more common modulation schemes will add sidebands that carry the information in the modulation. Depending on the amount and type of modulation with respect to the length of the FFT, the sidebands can either appear separate from the FFT peak, or just "fatten" a single peak.
Your spectrum will appear broadened and this happens in the real world. Look e.g for the Voight profile, which is a Lorentizan (the result of an ideal exponential decay) convolved with a Gaussian of a certain width, the width being determined by stochastic fluctuations, e.g. Doppler effect on molecules in a gas that is being probed by a narrow-band laser.
You will not get an 'ideal' frequency peak either way. The limit for the resolution of the FFT is one frequency bin, (frequency resolution being given by the inverse of the time vector length), but even that (as #xvan pointed out) is in general broadened by the window function. If your window is nonexistent, i.e. it is in fact a square window of the length of the time vector, then you'll get spectral peaks that are convolved with a sinc function, and thus broadened.
The best way to visualize this is to make a long vector and plot a spectrogram (often shown for audio signals) with enough resolution so you can see the individual variation. The FFT of the overall signal is then the projection of the moving peaks onto the vertical axis of the spectrogram. The FFT of a given time vector does not have any time resolution, but sums up all frequencies that happen during the time you FFT. So the spectrogram (often people simply use the STFT, short time fourier transform) has at any given time the 'full' resolution, i.e. narrow lineshape that you expect. The FFT of the full time vector shows the algebraic sum of all your lineshapes and therefore appears broadened.
To sum it up there are two separate effects:
a) broadening from the window function (as the commenters 1 and 2 pointed out)
b) broadening from the effect of frequency fluctuation that you are trying to simulate and that happens in real life (e.g. you sitting on a swing while receiving a radio signal).
Finally, note the significance of #xvan's comment : phi= phi(t). If the phase angle is time dependent then it has a derivative that is not zero. dphi/dt is a frequency shift, so your instantaneous frequency becomes f0 + dphi/dt.
I am trying to implement FFT, and I am OK with the code etc, but the general order of things is confusing me.
Am I right in thinking that this is the correct order of things to do?
Input -> Overlap input -> Windowing -> FFT -> Phase calculations/Overlap compensation -> Output
I'm getting results close to my input frequency, but they are consistently off by some factor that I can't work out, i.e. 440Hz is always 407Hz, 430Hz is always 420Hz.
The main bit that is confusing me is the initial overlap, as I have been looking at some open source FFT code and that is the part that I can never quite work out whats going on. I seem to be getting the idea from looking at those that overlapping is supposed to happen before windowing, but to me logically, wouldn't that mess with the windowing?
Any advice would be great
Thanks
The FFT is a discrete version of the continuous Fourier Transform.
The FFT produces a 1D vector of complex numbers. This complex vector is often used to calculate a 2D matrix of Frequency Magnitude versus Frequency, and represented as a 2D graph, like this one:
A single FFT is used when you want to understand the frequency spectrum of a signal. For example, from the above FFT graph we can say that most of the energy in this female soprano's G5 note is concentrated in the 784 Hz and 1572 Hz frequencies.
STFT or "Short-Time Fourier Transform" uses a sliding-frame FFT to produce a 2D matrix of Frequency versus Time, often represented as a graph called a Spectrogram, like this one:
The STFT is used when you want to know at what time a particular frequency event occurs in the signal. For example, from the above graph we can say that a large portion of the energy in this vocal phrase occurred between 0.05 and 0.15 seconds, in the frequency range of 100 Hz to 1500 Hz.
The workflow for the FFT is:
Sample the signal -> Window the entire sample frame -> FFT -> Calculate magnitude and phase -> Output something, usually a 2D graph
If your time-domain data is available in text form and if you can post it here, we can try to help you analyze it, or you can analyze it yourself with this online FFT: Sooeet FFT calculator
If you use window for FFT, your computation will be a kind of STFT.
There are some prepared codes of STFT like 'Spectrogram' etc.
To write the code by FFT, the overlapping is inevitable,but you can use some optimization methods to minimize ghost effects.Also, the practical way for windowing may be choosing the window's bandwidth according to frequency extension. It is clear that in high frequency data's you need to select small windows which is so time consuming.
I am not good enough in Matlab to write this code adhesively:)
Good Luck
I need to know a way to make FFT (DFT) work with just n points, where n is not a power of 2.
I want to analyze an modify the sound spectrum, in particular of Wave-Files, which have in common 44100 sampling points. But my FFT does not work, it only works with points which are in shape like 2^n.
So what can I do? Beside fill up the vector with zeros to the next power of 2 ?!
Any way to modify the FFT algorithm?
Thanks!
You can use the FFTW library or the code generators of the Spiral project. They implement FFT for numbers with small prime factors, break down large prime factors p by reducing it to a FFT of size (p-1) which is even, etc.
However, just for signal analysis it is questionable why you want to analyze exactly one second of sound and not smaller units. Also, you may want to use a windowing procedure to avoid the jumps at the ends of the segment.
Aside from padding the array as you suggest, or using some other library function, you can construct a Fourier transform with arbitrary length and spacing in the frequency domain (also for non-integer sample spacings).
This is a well know result and is based on the Chirp-z transform (or Bluestein's FFT). Another good reference is given by Rabiner and can be found at the above link.
In summary, with this approach you don't have to write the FFT yourself, you can simply use an existing high-performance FFT and then apply the convolution theorem to a suitably scaled and conditioned version of your signal.
The performance will still be, O(n*log n), multiplied by some implementation-dependent scaling factor.
The FFT is just a faster method of computing the DFT for certain length vectors; and a DFT can be computed for any length of input vector. You can also zero-pad your input vector to a length supported by your FFT library, which may be faster.
If you want to modify your sound file, you may need to use the overlap-add or overlap-save fast convolution filtering after determining the length of the impulse response of your frequency domain modification.
I have been developing a small software in .NET that takes a signal from a sensor in real time and takes the FFT of that signal which is also shown in real time.
I have used the alglib library for the FFT function. Now my purpose is to observe the intensity of some particular frequency in time.
In order to check the software, I provided a sine wave to its input having a frequency of 1 Hz. The following image shows the screen shot from the software. The upper graph shows the frequency spectrum showing the peak at 1 Hz. However, when this peak is observed in time, as shown in lower graph, the intensity behaves like a sine wave.
My sampling frequency is 30kHz. What I do not understand is how am I getting this sine signal and why is the magnitude of frequency behaving like this?
This is an example of the effects of Windowing. It derives from the fact that the FFT is not a precise operation except for when dealing with perfectly periodic signals. When you window your signal, you turn it into a smaller chunk that may not repeat perfectly. The FFT algorithm calculates the spectrum of this chunk of audio, repeated infinitely. Since it is not a perfect sine wave, you don't get an exact value for the result. Furthermore, we can see that if your window doesn't line up perfectly with a multiple of your signal frequency, then it will phase shift with respect to your signal, the window capturing a slightly different chunk of your signal, and the FFT calculating the spectrum of a different infinitely repeated signal. If you think about it, this phase difference will naturally be periodic as well, as the window catches up with the next period of your signal.
However, this would only explain smaller variations in the intensity. Assuming you used correct labels on the axes of the bottom graph (something you should double-check), something else is wrong. You're window might be too small (although I expect not, because then you would see more spectral bleeding). Another possibility that just occurred to me is that you might just be plotting the real part of the FFT, not the magnitude. As the phase changes, the real and complex parts might vary, but you'd expect the magnitude to stay roughly the same.