I am recording acceleration data with an MPU6050 connected to a Arduino1 and stored on a SD. Here you can find the code.
I need to calculate the FFT of an acceleration signal that was not sampled uniformly, so i have to resample my signal. After this suggestion I tried to study how to resample the signal.
From Mathworks documentation here it seems that I have to know the frequency of my signal and a nominal frequency. I read and studied the function resample
, but I don’t understand how it works very well. In my specific case how to define
x= sin(2*pi*f*T)
But in my code below I tried to implement the function (to give you more information for a detailed answer and prove my efforts, but I don't have enough reputation to post all the graph).
Then I studied gettdata
“Scattered interpolant class” finding not very useful in my situation, they seems to work for 2 or more dimensions.
So I followed another way, I used the function interp1
. I defined my equispaced vector using the mode and median value of my sample frequency, I discarded the average to avoid the influence of any peaks.
- Is a correct approximation?
- What kind of errors can occur?
This is my first time I approach to the FFT, and I have a civil engineer background, so is my first time with signal analysis, never studied Signal Theory before.
My interest for the FFT is to define the best low pass filter (example apply a Butterworth but I don’t know how to choose the filter order and cutoff frequency.
I don't have enough reputation to post more than 2 links sorry for that.
filename= uigetfile ('.txt');
fileID = fopen (filename);
logmpu6050 =csvread(filename);
fclose (fileID);
%Starting creating the specific Vectors
%Record Time in millisecond
time=logmpu6050(:,1);
%The x y z are converted to m/s^2
confactor=19.6/32768;
ax=logmpu6050(:,2);
confactor=19.6/32768;
ax=ax*confactor;
ay=logmpu6050(:,3);
ay=ay*confactor;
az=logmpu6050(:,4);
az=az*confactor;
%Define the sample rate subtracting from Sampletime i+1 Sampletime i
n=length(time);
for i = 1:n-1
samplerate(i) = time(i+1)-time(i);
end;
%I try to define the best frequency for "resample" function based on mode and median value
%I will not use average to avoid conditioning due to extreme value
f1=mode(samplerate);
f2=median(samplerate);
x1= sin(2*pi*f1*time);
x2= sin(2*pi*f2*time);
plot(time,x1,'.');
figure
plot(time,x2,'.');
%resampling at time mode record frequency
[test1,test2]= resample(az,time,f1);
%resampling at time median record frequency
[test3,test4]= resample(az,time,f2);
figure
plot(test2, test1)
figure
plot(test4,test3)
f1=int32(mode(samplerate));
f2=int32(median(samplerate));
%Try to implement the matlab function "interp1"
%My input are
%x-> time when the value is recorded "time"
%v-> the acceleration value "az"
%xq-> I tried two define two type of equispaced vectors
%xq1-> Defined by the total recording time divided by the mode
%xq2-> Defined by the total recording time divided by the median
totrectime= int32(logmpu6050(n,1)-logmpu6050(1,1)) %milliseconds
numsamples1= idivide(totrectime,f1); %Rounded toward zero to the nearest integers.
numsamples2= idivide(totrectime,f2);
f1double=double(f1);
f2double=double(f2);
for i = 1: numsamples1
xq1(i)= f1double*i;
end;
for i = 1:numsamples2
xq2(i)= f2double*i;
end;
xq1double=double(xq1);
xq2double=double(xq2);
vq1 = interp1(time,az,xq1double);
vq2 = interp1(time,az,xq2double);
figure
plot(xq1double,vq1);
figure
plot(xq2double,vq2);