cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

FFT Confusion?

JohnRudnicki
15-Moonstone

FFT Confusion?

I was testing the fft and ifft routines on a gaussian function. Seems to work fine - I get original function back applying fft and ifft. But the discrete function I get from fft does not look anything like the transformed function given by symbolic evaluation. What am I missing here? (see attached worksheet).

ACCEPTED SOLUTION

Accepted Solutions
LouP
12-Amethyst
(To:JohnRudnicki)

The main error in your calc was that you used f instead of omega in the computation of the symbolic transform values. Once this is fixed, the freq scaling error between the symbolic and numeric disappears. See attached.

I also changed the positioning variable "a" to be a fraction of the overall time period, rather than depend directly on the index (number of pts). In general, I find it much more useful and less confusing to set up problems so that the quantization from continuous to discrete - setting the number of pts - is done so that the computation is always done on the basis of quantities that have meanig to the problem - time,volts, length, etc - rather than directly as a function of some index. The attached is set up now so that you can vary the number of points without affecting most parameters. I also added a DC offset term.

I created some plots at the bottom whcih concatenate three periods of time and freq domain info, so you can see how the sections match at the edges. I always find this useful when trying to interpret DFT results.

Using the cfft (or CFFT, which differs only in the amplitude scaling) as you did here, allows the use of arbitrary number of pts, not just powers of two. Try N0=28, and notice the repeated time waveform at the bottom which is almost sinusoidal, and the corresponding spectrum. Them try setting DC = -0.5 to remove the offset, and notice that only the fundamental complex freqs (f=-1, +1) are significant, as we would expect.

Lou

View solution in original post

5 REPLIES 5

I searched for the definition of your vector "f_bar" and could not find it. (So your three plots on the bottom only had one trace each.)

The numeric FFT functions (fft, FFT) don't know anything about frequency, they're only operating on one vector. I don't know how you'd treat a negative frequency.

LouP
12-Amethyst
(To:JohnRudnicki)

The missing piece is that the two vectors related by a DFT (discrete Fourier transform), in any of its guises, each represent a single period of a sampled, periodic waveform; the key word being "periodic." The symbolic transform is not the DFT, but the continuous transform, so you need to be aware of the differences.

The periodic repaeat of your Gaussian vector "v" is almost sinusoidal at the overall period, hence it has a DFT with components at p=0 (DC avg), at p=1 (fundamental component), a smaller component at p=2 (some 2nd harmonic since Gaussian is not a perfect sine), and very little else.

If you are modeling a single pulse/time event using DFT's, you need to account for the fact that the actual math of the DFT treats the input as a definition of a single period of a periodic waveform. One of the ways I use in modeling is to pad the vector length so that the single time(or space) event of interest is a small part of the overall period. Padding with a long string of zeroes works, and you need to check what is actually needed depending on the application.

Try your example after padding v up to 1024 with zeroes, and look at the magnitude of the resulting DFT.

As Fred points out, the DFT routines do not know about freq, so you need to keep track yourself.

Another point of caution: the DFT's are not "zero-centered." The time vector origin is the first point of the vector, so any waveform centered within the def of the vector has an inherent time offset of N/2 (N = vector length), which is a delay. This will show up as a freq dependent phase rotation mixing up what you may expect for real and imaginary parts. Also, the resulting DFT freq vector maps from f=0 to f=fs (sampling freq), not from -fs/2 to +fs/2. at least for the CFFT and ICFFT DFT versions. I think the fft just returns from f=0 to f=fs/2, and is restricted to real waveform inputs, and don't allow for negative freq. For the CFFT-ICFFT routines, the upper half of the freq vector (index N/2 to N-1) corresponds to the "negative" freqs f= -fs/2 to f=0(almost). You need to construct the freq vector appropriately, making sure that the components are at the correct indices in the vector, befor applying the nverse DFT.

Lou

JohnRudnicki
15-Moonstone
(To:LouP)

Thanks Lou and Fred. Sorry about the omission in the originally posted worksheet. I am a little less confused but still unclear. Attached worksheet is extracted from the Quicksheet and has essentially the same example I was trying to construct in the previously posted ws. Analytic form of the transform of a gaussian (via symbolic processor) is compared with the result of the discrete cfft.

The missing piece is that the two vectors related by a DFT (discrete Fourier transform), in any of its guises, each represent a single period of a sampled, periodic waveform; the key word being "periodic." The symbolic transform is not the DFT, but the continuous transform, so you need to be aware of the differences.

The periodic repaeat of your Gaussian vector "v" is almost sinusoidal at the overall period, hence it has a DFT with components at p=0 (DC avg), at p=1 (fundamental component), a smaller component at p=2 (some 2nd harmonic since Gaussian is not a perfect sine), and very little else.

If you are modeling a single pulse/time event using DFT's, you need to account for the fact that the actual math of the DFT treats the input as a definition of a single period of a periodic waveform. One of the ways I use in modeling is to pad the vector length so that the single time(or space) event of interest is a small part of the overall period. Padding with a long string of zeroes works, and you need to check what is actually needed depending on the application.

Try your example after padding v up to 1024 with zeroes, and look at the magnitude of the resulting DFT.

I think I understand some of what you are saying here - gaussian is not periodic but discrete fft implicitly assumes a periodic function? But in the example, the Gaussian is sampled over a large range (maybe not large enough?) where the function is essentially zero. Isnt this the same as your suggestion of padding? Presumably the continuous (analytic) and discrete versions should approach each other in some limit but how to see this numerically?

Another point of caution: the DFT's are not "zero-centered." The time vector origin is the first point of the vector, so any waveform centered within the def of the vector has an inherent time offset of N/2 (N = vector length), which is a delay. This will show up as a freq dependent phase rotation mixing up what you may expect for real and imaginary parts. Also, the resulting DFT freq vector maps from f=0 to f=fs (sampling freq), not from -fs/2 to +fs/2. at least for the CFFT and ICFFT DFT versions. I think the fft just returns from f=0 to f=fs/2, and is restricted to real waveform inputs, and don't allow for negative freq. For the CFFT-ICFFT routines, the upper half of the freq vector (index N/2 to N-1) corresponds to the "negative" freqs f= -fs/2 to f=0(almost). You need to construct the freq vector appropriately, making sure that the components are at the correct indices in the vector, befor applying the nverse DFT.

Is "re-centering" an issue here? Doesnt this only affect how the negative frequencies are presented?

LouP
12-Amethyst
(To:JohnRudnicki)

The main error in your calc was that you used f instead of omega in the computation of the symbolic transform values. Once this is fixed, the freq scaling error between the symbolic and numeric disappears. See attached.

I also changed the positioning variable "a" to be a fraction of the overall time period, rather than depend directly on the index (number of pts). In general, I find it much more useful and less confusing to set up problems so that the quantization from continuous to discrete - setting the number of pts - is done so that the computation is always done on the basis of quantities that have meanig to the problem - time,volts, length, etc - rather than directly as a function of some index. The attached is set up now so that you can vary the number of points without affecting most parameters. I also added a DC offset term.

I created some plots at the bottom whcih concatenate three periods of time and freq domain info, so you can see how the sections match at the edges. I always find this useful when trying to interpret DFT results.

Using the cfft (or CFFT, which differs only in the amplitude scaling) as you did here, allows the use of arbitrary number of pts, not just powers of two. Try N0=28, and notice the repeated time waveform at the bottom which is almost sinusoidal, and the corresponding spectrum. Them try setting DC = -0.5 to remove the offset, and notice that only the fundamental complex freqs (f=-1, +1) are significant, as we would expect.

Lou

JohnRudnicki
15-Moonstone
(To:LouP)

Lou: Many thanks for your clear and detailed tutorial.

(BTW, as for being "old", I suspect you do not have much on me there, but, sometimes, even old dogs can learn a new trick or two).

Announcements

Top Tags