MATLAB: Converting accelerometer data to frequency

accelerometercodefrequencyimuMATLABtime

Hi, I'm trying to take accelerometer data I recorded with an Adafruit BNO05 IMU and generate two graphs: one that plots acc vs. time and one that plots acc vs. freq. I'm attaching a data file for reference. Our current code doesn't seem to be working quite right for the acc vs. freq part. I'm also attaching that code. Any suggestions for how we can fix the code?
%Reads raw data from the xlsfile.
function [acceleration,time] = TremorReader(filename)
%Open the Excel file and extract information
[num text raw ] = xlsread(filename) ;
[r,c] = size(num) ;
%Delete unecessary information
num(2:2:end,:) = [] ;
num(:,2) = [] ;
num(:,5) = [] ;
%Extract time
time = num(:,1) ;
time = time - time(1) ;
%convert from millisecond to seconds
timesec = time./1000 ;
%replace in data again
num(:,1) = timesec ;
%Extract all axes
ax = num(:,2) ;
ay = num(:,3) ;
az = num(:,4) ;
%Compute the cumulative/magnitude vector
acc = sqrt(ax.^2 + ay.^2 + az.^2); %possibly in G's
acc_cmpersec = acc.*980; %converts to cm/s^2
hold on
subplot(1,2,2);
plot(timesec,acc);
dt = .103;%time between samples????
N1 = length(acc);
N = 2^nextpow2(N1);
if N > N1
acc(N1+1:N) = 0; % pad array with 0's
end
df = 1 / (N*dt); % frequency increment
Nyq = 1 / (2*dt); % Nyquist frequency
freq = fftshift(fft(acc));
freq = sort(freq);
mag_freq = sqrt((real(freq).^2)+(imag(freq).^2)); %magnitude of the frequency, combines the real and imaginary parts
%M = abs(freq), this is the same as mag_freq, just a different method
phase_freq = angle(freq);
subplot(1,2,1);
plot(mag_freq, acc);
%hold on
% used plot(freq,acc,'r') before (other night)
%hold off
RMSacc = norm(acc_cmpersec)./sqrt(length(acc_cmpersec)); %root mean square to combine the acceleration vectors into one value for the amplitude and score
Dom_freq = max(mag_freq); %taking the dominant frequency
T = RMSacc./((2.*pi.*Dom_freq ).^2); %translational displacement
amplitude = abs((T)./(sin(2.*pi*Dom_freq .*4)));
%if amplitude==0;
% score=0;
% elseif amplitude<1;
% score=1;
% elseif amplitude>=1 & amplitude<3;
% score=2;
% elseif amplitude>=3 & amplitude<10;
% score=3;
% else
% score=4;
% end
%disp(score); %These will be used to find TP,TN,FP, and NP
end

Best Answer

Try this:
num = xlsread('test550.csv');
num = num(1:2:end,:); % Eliminate ‘NaN’ Values
num(:,1) = num(:,1)*1E-3; % Convert Milliseconds To Seconds
Fs = 1/mean(diff(num(:,1))); % Sampling Frequency (Convert Milliseconds To Seconds)
Fn = Fs/2; % Nyquist Frequency
Tr = linspace(num(1,1), num(1,end), size(num,1)); % Time Vector (Regular Samples)
Dr = resample(num(:,2:end), Tr); % Resample To Constant Sampling Interval
Dr_mc = Dr - mean(Dr,1); % Subtract Mean
FDr_mc = fft(Dr_mc, [], 1); % Fourier Transform
Fv = linspace(0, 1, fix(size(FDr_mc,1)/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FDr_mc(Iv,:))*2)
grid
hl = legend('B','C','D','E','F', 'Location','NE')
title(hl, 'Excel Columns')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
Experiment to get the result you want.