# Generating Frequency domain with FFT function of accelerometer data

조회 수: 14 (최근 30일)
An Van . 2020년 7월 7일
댓글: An Van . 2020년 8월 16일
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;
% 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
Thank you.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Raunak Gupta 2020년 8월 8일
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
##### 댓글 수: 1표시 없음숨기기 없음
An Van 2020년 8월 16일
Thank you, I could finally get this part done.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!