Generating Frequency domain with FFT function of accelerometer data
    4 views (last 30 days)
  
       Show older comments
    
Hello,
I have accelerometer data that are recorded in excel sheet.
I would like to extract the frequency domain features from the data. 
In order to do that, I need to convert the data into frequency by using the FFT function.
First of all, I generate the whole frequency of the data with this code:
freX = fft(accX)/N;
freX_2 = abs(freX);
freX_1 = freX_2(1:N/2+1);
freX_1(2:end-1)= 2*freX_1(2:end-1);
f_freX = fs*(0:(N/2))/N;
It works fine.
Now, I need to cut the data into windows with 200 data point for each window.
However, my code is hitting errors and I couldnt debug it.
for i =1:number_of_windows % there are 700 windows
   % generating the frequency domain
   a = fft(accX((1+(i-1)*s_win):(i*s_win)));
   freX_2 = abs(a/N); 
   freX_1 = freX_2(1:N/2+1); 
   %error: Index exceeds the number of array elements (200).
   % extracting the frequency domain features
   meanfreX(i) = meanfreq(freX_1(1+(i-1)*s_win):(i*s_win)); % mean frequency
   peakX(i) = findpeaks(freX_1(1+(i-1)*s_win):(i*s_win)); % peak frequency
   medfreX(i) = medfreq(freX_1(1+(i-1)*s_win):(i*s_win)); % median frequency
   powX(i) = ((abs(freX_1(1+(i-1)*s_win):(i*s_win))).^2)/s_win; %power spectrum
end
My whole code look like these:
clear all;
close all;
% Load the data for treadmill subject
t1= xlsread('data1',1);
% 1st column is accelerometer on X axis
accX = t1(:,1) ;
% set up window length
window = 2;
% set up frequency signal
fs = 100;
% set up size window
s_win = window*fs;
N = length(accX);
number_of_windows = floor(N/s_win);
% Fourier Transform
for i =1:number_of_windows % there are 700 windows
   % generating the frequency domain
   a = fft(accX((1+(i-1)*s_win):(i*s_win)));
   freX_2 = abs(a/N); 
   freX_1 = freX_2(1:N/2+1); 
   %error: Index exceeds the number of array elements (200).
   % extracting the frequency domain features
   meanfreX(i) = meanfreq(freX_1(1+(i-1)*s_win):(i*s_win)); % mean frequency
   peakX(i) = findpeaks(freX_1(1+(i-1)*s_win):(i*s_win)); % peak frequency
   medfreX(i) = medfreq(freX_1(1+(i-1)*s_win):(i*s_win)); % median frequency
   powX(i) = ((abs(freX_1(1+(i-1)*s_win):(i*s_win))).^2)/s_win; %power spectrum
end
Please help me debug the fourier transform.
Thank you.
0 Comments
Accepted Answer
  Raunak Gupta
    
 on 8 Aug 2020
        Hi, 
In the line 
freX_1 = freX_2(1:N/2+1); 
freX_2 is built to have 200 elements but when you are calculating freX_1, the indexing is done with N/2 which is essentially the size of full accX and not of freX_2. In the above line if you replace N with s_win, I think the error will be resolved. But again when I see down you have taken freX((1+(I-1)*s_win) :(i*s_win)) which can be correct if freX_1 has size s_win. I think the code in the for loop should only work for a single window. Also you have used findpeaks which will return variable number of frequencies as local maxima. Instead I think you want max frequency so I have replaced it with max(freX_1) for it work. 
So, I think the for loop may be written like below for it to work.
for i =1:number_of_windows % there are 700 windows 
    % generating the frequency domain 
    a = fft(accX((1+(i-1)*s_win):(i*s_win))); 
    freX_2 = abs(a/s_win);  
    freX_1 = freX_2(1:s_win/2+1);  
    % extracting the frequency domain features 
    meanfreX(i) = meanfreq(freX_1); % mean frequency
    peakX(i) = max(freX_1); % peak frequency 
    medfreX(i) = medfreq(freX_1); % median frequency 
    powX(i) = (sum(abs(freX_1).^2))/s_win; %Average power spectrum 
end
More Answers (0)
See Also
Categories
				Find more on Spectral Measurements in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
