Below is a script that I have been using to create an averaged and windowed FFT:
function ff = fft_avg(x, M)
if (nargin == 1)
M = length(x);
N = length(x);
win = blackman(M);
%win = hanning(M);
%win = hamming(M);
%% calculate average FFT, with window size M
This has been cobbled together from multiple scripts, and if anyone has any input, it would be appreciated.
Thanks for contributing this code. We were planning to put in windowing in front of our power spectra, next.
Another way to improve the powerspectrum example is to use, say, a Hanning window and then step through the data only 1/2 of an FFT_length at a time. As you know, window functions reduce boundary artifacts in your FFT, but they effectively throw away 1/2 of the data (SNR drops by root(2), your mileage varies depending on which window you use). The correct way to handle this deficiency is to perform 2x as many FFT's, stepping only one half as far each time. By reducing the step size, you make sure no data is lost and recover the correct total power at the end.
Want to modify the Ocave code and post it?
Good idea, I'll try to get to that soon.... I'll see if I can't work it into your Power Sepctrum code too.
While I like the new forum format, is this the best way to post code? Sometimes it is hard to find that snippet of code that lurks deep within the forum...
Clearly this is not the best way to post code. I would like to have the ability to attach documents to posts. Let me ask you and the community: Would document attachements be the best way to post code snippets (or other things, like graphics)?
Code that is not suitable for being posted in a source repository I prefer to get in some kind of plain text form (I've used pastebin.com here on the forum a couple of times). Graphics I prefer to get as PNG or JPG, so they can be accessed directly in the browser (imageshack.us and similar services will host them).
Something like this would be ideal, but it may not be easy to set up.
If I look at the other forums I read, attachments are often used. It would be nice if graphics/plots would appear without having to click a link.
I guess for small code snippets, the forum is OK (that is common on other Matlab/octave forums).
How about a GIT repository?
Here is an updated FFT averaging function, rewritten and made easier to understand.
Again, feedback is appreciated! I *think* it is working correctly now, with the 1/2 scale windowing. :)
function y = fft_avg2(x, win_length)
if (nargin == 1)
win_length = 4096;
step = win_length / 2;
fft_win = hanning(win_length)';
y = zeros(1,win_length);
start = 1;
stop = win_length;
while (stop < length(x) - step)
y = y + abs(fft(x(start:stop).*fft_win));
start = start + step;
stop = stop + step;