DSPRelated.com
Forums

Question about DSP in Matlab (beginner)

Started by JCO_DSP May 23, 2015
Hi,

I'm starting to learn DSP (Matlab )and I have two questions about it,
maybe someone can help me out?

I'm trying to measure a frequency from an Audio Device and I'm using FFT
to transform it from the time domain to the frequency domain. But I am
doing a downsample before the FFT. But you can also do it after the FFT.
Do you know what would be the most efficient (before or after)? And why?

Another question, I am using zero padding to increase my system resolution
(up to 50Hz). My frame length is N=256 and sample frequency fs= 44100 Hz.
So 44100/50 = 882, so I need to add 626 zeroes (to 256 frame length) to
get 50 Hz resolution, I hope this is correct? I believe this is done for
FFT. But is this the same for the DFT?

Thank you and regards,



---------------------------------------
Posted through http://www.DSPRelated.com
>Hi, > >I'm starting to learn DSP (Matlab )and I have two questions about it, >maybe someone can help me out? > >I'm trying to measure a frequency from an Audio Device and I'm using FFT >to transform it from the time domain to the frequency domain. But I am >doing a downsample before the FFT. But you can also do it after the FFT. >Do you know what would be the most efficient (before or after)? And >why? > >Another question, I am using zero padding to increase my system >resolution >(up to 50Hz). My frame length is N%6 and sample frequency fs= 44100 >Hz. >So 44100/50 = 882, so I need to add 626 zeroes (to 256 frame length) to >get 50 Hz resolution, I hope this is correct? I believe this is done for >FFT. But is this the same for the DFT? > >Thank you and regards, > > > >--------------------------------------- >Posted through http://www.DSPRelated.com
If you have a single tone that you are trying to measure, you don't have to do any zero padding. I recommend you read my blog article titled "Exact Frequency Formula for a Pure Real Tone in a DFT" at dsprelated (dot) com. You want to make sure that your sample frame is long enough to hold at least a few cycles of your signal. With 256 points, that means your tone has to be greater than 172 Hz to get at least one full cycle. My formula will actually work on a partial cycle if you center the bins on bin one. The accuracy of the answer will be significantly better than 50hz. If you have a clean signal, 256 points is overkill. If your tone is below 172 Hz, your "downsampling" can consist of grabbing every other sample, or every third, depending upon how much you need to lengthen your frame. If you use every fourth point, one cycle per frame will be at about 43 Hz. Your upper limit (Nyquist frequency) will be about 5512 Hz. An FFT is a DFT. By having a power of two size, there are calculation short cuts that can be taken. Hope this helps. I recommend you read all of my blogs. They are meant to give an understanding to the DFT. You can get to them by clicking on the red "Cedron Dawg" label. Ced --------------------------------------- Posted through http://www.DSPRelated.com
You can readily experiment with matlab fft and notice that:

For a single tone you need to have at least one complete sine cycle of
tone and fft resolution equal to one cycle to get accurate result (power
in single bin, zero elsewhere). Or you can have multiple cycles and fft
equal to any number of given complete cycles. If fft is more than
available vector length then matlab inserts zeros and distorts results due
to resulting discontinuity.

fft views input as wrapping up and so phase discontinuity is seen by fft
leading to false high frequencies.In fact, for any signal you also need to
avoid discontinuity in the original vector generation else use windowing
to suppress discontinuity. If signal has no discontinuity then applying
window will distort result.

For any bandlimited signal The higher the fft resolution the better the
accuracy. But if you require specific fft resolution then you can just go
for that at whatever fft resolution is required.

zero padding will cause dsicontinuity and lead to false power in adjacent
bins.
Inserting zeros does not add new information but will modify resolution.
Practically I don't see anything but disadvantage of zero insertion and is
apparently used by matlab for speeding up fft (as power of 2 DFT is faster
than non ower of 2)


Kaz
---------------------------------------
Posted through http://www.DSPRelated.com
[...snip...]
> >For any bandlimited signal The higher the fft resolution the better the >accuracy.
This is simply not true unless there is noise present. If you have a signal that has four cycles in your interval, the DFT will have a value in bin four no matter how many samples you use. Suppose the signal has 4.3 cycles in your interval, then the bins around bin four will have values. Unlike the whole frequency case, these bin values will vary a little bit with the number of samples. So, if you are using a frequency estimation formula, the more bins the better. If you use the exact formula, the number of samples are accounted for in the formula. Check out my blog article I referenced previously. I get fourteen significant digits of accuracy from a 32 bin DFT. What extra samples does for you is: 1) Rule out alias frequencies 2) Mitigate the effects of noise [...snip...] Ced --------------------------------------- Posted through http://www.DSPRelated.com
>[...snip...] >> >>For any bandlimited signal The higher the fft resolution the better >the >>accuracy. > >This is simply not true unless there is noise present. If you have a >signal that has four cycles in your interval, the DFT will have a value >in >bin four no matter how many samples you use. Suppose the signal has 4.3 >cycles in your interval, then the bins around bin four will have values. >
Thanks for the comments Cedron. I have read your article. I have already pointed out that in the case of single tone if you have multiple complete cycles of a tone then fft of length one cycle or multiple cycles yields same accuracy (one bin with power, others zero) and as such fft resolution does not increase accuracy. For a bandlimited signal having "continuous" frequencies across from edge to to edge a higher fft resolution means just that; higher frequency resolution or am I wrong? I hope I understood your point. --------------------------------------- Posted through http://www.DSPRelated.com
>[...snip...] >> >>For any bandlimited signal The higher the fft resolution the better >the >>accuracy. > >This is simply not true unless there is noise present. If you have a >signal that has four cycles in your interval, the DFT will have a value >in >bin four no matter how many samples you use. Suppose the signal has 4.3 >cycles in your interval, then the bins around bin four will have values. > >Unlike the whole frequency case, these bin values will vary a little bit >with the number of samples. So, if you are using a frequency estimation >formula, the more bins the better. If you use the exact formula, the >number of samples are accounted for in the formula. > >Check out my blog article I referenced previously. I get fourteen >significant digits of accuracy from a 32 bin DFT. > >What extra samples does for you is: > >1) Rule out alias frequencies > >2) Mitigate the effects of noise > >[...snip...] > >Ced >--------------------------------------- >Posted through http://www.DSPRelated.com
Thank you Ced and Kaz, Also thanks for the link about "Exact Frequency Formula for a Pure Real Tone in a DFT". Regards, JCO --------------------------------------- Posted through http://www.DSPRelated.com
> >Thanks for the comments Cedron. I have read your article. >I have already pointed out that in the case of single tone if you have >multiple complete cycles of a tone then fft of length one cycle or >multiple cycles yields same accuracy (one bin with power, others zero) >and >as such fft resolution does not increase accuracy. > >For a bandlimited signal having "continuous" frequencies across from >edge >to to edge a higher fft resolution means just that; higher frequency >resolution or am I wrong? > >I hope I understood your point. > >--------------------------------------- >Posted through http://www.DSPRelated.com
Kaz and JCO_DSP, Thank you both for the positive responses. Kaz, I do believe you have misunderstood my point. Just to be clear, we are talking about a fixed time interval, and increasing the sample density. As long as the frequencies are below the Nyquist limit, you do not get any better frequency resolution, meaning being able to discern (resolve) two different tones, by increasing the number of samples. For instance, if there is one tone at a frequency of 4.3 (cycles per frame) and another at 4.8, they are going to end up mostly in bins four and five no matter how many samples you take. Difficult to resolve. In order to make them easier to resolve you need to increase the interval size. If you were to quadruple the interval length (assuming the tones stay steady) the frequencies would now be 17.2 and 19.2 respectively, easily resolved in a DFT. Assuming again that there is only one tone, you would want to have at least two cycles within your interval. This is because no tone is going to be pure and you want the harmonics at least one bin away. The exact formula in my blog article pertains to a single pure tone. I will be handling the multiple tone case in a few articles hence. I am not sure what you mean by "continuous frequency from edge to edge". Yes, if you have a single whole integer pure tone (perfect sinusoidal) frequency, then the DFT (below the Nyquist bin) will zero everywhere except the matching bin. Then it doesn't matter how many cycles there are. If it is a noiseless non-integer frequency pure tone, the frequency formula will be just as accurate no matter what the sampling density. Ced --------------------------------------- Posted through http://www.DSPRelated.com
On 5/23/15 6:03 PM, kaz wrote:
>> [...snip...] >>> >>> For any bandlimited signal The higher the fft resolution the better >>> the accuracy. >> >> This is simply not true unless there is noise present.
i don't even know it's true even if there is noise present. the information is contained in the non-zero samples before any zero-padding to increase FFT resolution. the accuracy is dependent solely upon that. other interpolation methods might be used to get as good of resolution. On 5/23/15 6:11 PM, JCO_DSP wrote:
> > Thanks for the comments Cedron. I have read your article.
i didn't see a link to an article.
> > Thank you Ced and Kaz, > > Also thanks for the link about "Exact Frequency Formula for a > Pure Real Tone in a DFT".
i presume this is a sinusoidal tone. if it is, then i think it's a solved problem if you know what the window is used before the DFT. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
> >Kaz, I do believe you have misunderstood my point. Just to be clear, we >are talking about a fixed time interval, and increasing the sample >density. >
Ced I never heard of this terminology, I still think you mean DFT (of a time sequence of samples), no problem.
>As long as the frequencies are below the Nyquist limit, you do not get >any better frequency resolution, meaning being able to discern (resolve)
two
>different tones, by increasing the number of samples. >
On this one, I have been scratching me head since last night. Doesn't everybody say fft frequency resolution = Fs/N (= bin width)??? Or is your concept of resolution something else? That is what I meant by accuracy, may be not best wording but should be ok for a practical DSP beginner. Kaz --------------------------------------- Posted through http://www.DSPRelated.com
[...snip...]
>On 5/23/15 6:11 PM, JCO_DSP wrote: >> >> Thanks for the comments Cedron. I have read your article. > >i didn't see a link to an article. >
Technically, I didn't give a link, I gave a reference to "read my blog articles at dsprelated (dot) com." Asking the Google with the title would have gotten you a link, but for your convenience it is: http://www.dsprelated.com/showarticle/773.php
>> >> Thank you Ced and Kaz, >> >> Also thanks for the link about "Exact Frequency Formula for a >> Pure Real Tone in a DFT". > >i presume this is a sinusoidal tone. if it is, then i think it's a >solved problem if you know what the window is used before the DFT. >
Well, I think you think wrong on this one. If it has been solved, it should be mentioned on these pages, don't you think? http://www.dspguru.com/dsp/howtos/how-to-interpolate-fft-peak http://www.ericjacobsen.org/fe2/fe2.htm
> >-- > >r b-j rbj@audioimagination.com > >"Imagination is more important than knowledge."
I think paradigm shifts are a hoot. How about you? Ced --------------------------------------- Posted through http://www.DSPRelated.com