Saturday 8 August 2015

matlab - Average FFT Magnitude in bins


I am analyzing sounds of daily activities recorded by a smartphone. For example walking, getting up from bed, falling, running etc.. (one at the time). Let’s take one them. My goal is to




  • convert from time domain to frequency domain and then

  • divide it in bins of 10 Hz wide

  • calculate the average FT magnitude in each of these bins.


Questions:




  1. When I use fft Matlab function, Do I have to choose NFFT>N and power of 2 (Y = fft(signal,NFFT)/N;), or can I just use Y = fft(signal); ?





  2. In my case, is signal = detrend(signal); necessary before doing FFT?




  3. In order to have 10 Hz wide bins, using 4096 Hamming Window is the way to do so?




  4. Do my plots need further processing in order to achieve my goal?





enter image description here


[signal,fs]=audioread(filename); % fs = 44100
N = length(signal); % N = 94144

%% Amplitude Spectrum

Y = fft(signal);
Y_mag = abs(Y);
Fbins = ((0: 1/N: 1-1/N)*fs).';
figure;

subplot(4,1,1);
plot(Fbins,Y_mag);
title('FFT MAGNITUTUDE REPONSE');
xlabel('FREQUENCY (HERTZ)');
ylabel('MAGNITUTDE');

%% Single-Sided Amplitude Spectrum
NFFT = 2^nextpow2(N);
Y_NFFT = fft(signal,NFFT)/N;
f_NFFT = fs/2*linspace(0,1,NFFT/2+1);

subplot(4,1,2);
plot(f_NFFT,2*abs(Y_NFFT(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')


%% Pwelch defaut values
subplot(4,1,3);
pwelch(signal,[],[],[],fs);

title(sprintf('Pwelch defaut values'));

%% Pwelch Hamming Window 4096
len = 4096; % different sizes of Hamming Window
subplot(4,1,4);
pwelch(signal,len,[],len,fs);
title(sprintf('Pwelch Hamming Window Size :: %d',len));

Thank you very much



Answer





When I use fft Matlab function, Do I have to choose NFFT>N and power of 2 (Y = fft(signal,NFFT)/N;), or can I just use Y = fft(signal); ?



Not necessarily, but it is better to have it in power of 2 for faster computations. Also NFFT>N will make your response more clearer (just for visual purpose) as the frequency resolution doesnot changes with NFFT>N. You can also look into zero padding as it is what you will achieve with NFFT>N.


As also mentioned in the document of FFT:


The execution time for fft depends on the length of the transform. The
time is fastest for powers of two and almost as fast for lengths that
have only small prime factors. The time is typically several times
slower for lengths that are prime, or which have large prime factors.



In my case, is signal = detrend(signal); necessary before doing FFT?



It depends on whether you want to measure the frequency component of 0-10 Hz range (specifically from your description) If DC component is of no need then you can perform signal = detrend(signal);



In order to have 10 Hz wide bins, using 4096 Hamming Window is the way to do so?



NO, first of all windowing is used to decrease the side loobes which occurs due to spectral leakage. There is a effect of response broadening due to side level suppression but I think this is not what you needed. Also do remember that windowing will be applied on raw time signal before zero padding. You have to play with number of samples to have your desired resolution



Do my plots need further processing in order to achieve my goal?




I cannot guess this, but might be from the above answers you can estimate what has to be done in order to achieve your specific goal.


Hope that this answer might help.


No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...