Converting tremor data in to frequency and filter data
Show older comments
Can anyone help in apply fft and filtering to my IMU sensor data for detecting tremor
4 Comments
Jeffrey Clark
on 13 Sep 2022
@Paras Shaikh, the csv you provided doesn't provide time readings or units for the measurements, and should we just assume from the last column that there are three separate datasets? If the samples are at the exact same spacing of time please provide that and the acceleration and position units. Are the three sets related and to be evaluated over the same time period or just stuck in one file for convieniance?
Star Strider
on 13 Sep 2022
Edited: Star Strider
on 13 Sep 2022
I need a corresponding time vector (preferably) and if the data are regularly-sampled, the sampling frequency.
Also, what is the ‘label’ variable, and is it important?
Paras Shaikh
on 14 Sep 2022
Star Strider
on 14 Sep 2022
Answers (1)
Star Strider
on 13 Sep 2022
Edited: Star Strider
on 14 Sep 2022
The fft plots are straightforward, however only the acceleration signals make sense. The ‘Gyr’ signals exhibit broadband noise.
How do you want to process them?
One approach —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1124040/labeled%20final%20data.csv')
Acc = T1{:,1:3}
Gyr = T1{:,4:6};
Data = [Acc Gyr];
Fs = 1; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
NrSp = size(Data,2); % Number Of Subplots
L = size(Data,1); % Length Of Data Vectors
t = linspace(0, L-1, L)/Fs; % Time Vector
NFFT = 2^nextpow2(L); % For Efficiency
FTData = fft(Data - mean(Data),NFFT)/L; % Fopurier Transform
Fv = linspace(0, 1, NFFT/2-1)*Fn; % Frequency Vector (For Plots)
Iv = 1:numel(Fv); % Index Vector (For Plots)
figure
sp = [1:2:NrSp 2:2:NrSp]; % Order 'subplot' Plots
for k = 1:NrSp
subplot(3,2,sp(k))
plot(Fv, abs(FTData(Iv,k))*2) % Plot Fourier Transforms
grid
xlabel('Frequency')
ylabel('Magnitude')
xlim([0 Fn/20])
% xlim([5 10])
title(sprintf('Column %d',k))
end
sgtitle('Fourier Transform of Data')
figure
sp = [1:2:NrSp 2:2:NrSp];
for k = 1:NrSp
subplot(3,2,sp(k))
plot(t, Data(:,k))
grid
ylim([-3 3])
xlabel('Time')
ylabel('Amplitude')
title(sprintf('Column %d',k))
end
sgtitle('Original Time-Domain Data')
Wp = [0.0001 0.005]/Fn; % Define Passband In Hz, Normalise To (0,pi)
Ws = Wp.*[0.95 1.05]; % Stopband (Normalised)
Rs = 50; % Stopband Ripple (Attenuation) dB
Rp = 1; % Passband Ripple dB
[n,Wn] = ellipord(Wp,Ws,Rp,Rs); % Calculate Filter Order
[z,p,k] = ellip(n,Rp,Rs,Wp); % Design Filter
[sos,g] = zp2sos(z,p,k); % Implement Filter As Second-Order-Section Representation
figure
freqz(sos, 2^16, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 0.01])
set(subplot(2,1,2), 'XLim',[0 0.01])
Data_Filt = filtfilt(sos,g,Data);
figure
for k = 1:NrSp
subplot(3,2,sp(k))
plot(t, Data_Filt(:,k))
grid
ylim([-1 1]*0.75)
xlabel('Time')
ylabel('Amplitude')
title(sprintf('Column %d',k))
end
sgtitle('Time-Domain Plots of Bandpass (0.0001 - 0.005 Hz) Filtered Data')
EDIT — (14 Sep 2022 at 11:40)
Changed ‘Fs’ and filter passbands to conform to the 1 Hz sampling frequency.
.
12 Comments
Paras Shaikh
on 14 Sep 2022
My pleasure!
They will overplot each other if plotted on the same axes, so I would use subplot plots in one figure.
Try something like this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1124040/labeled%20final%20data.csv')
Acc = T1{:,1:3};
Gyr = T1{:,4:6};
Data = Acc;
Fs = 1; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
NrSp = size(Data,2); % Number Of Subplots
L = size(Data,1); % Length Of Data Vectors
t = linspace(0, L-1, L)/Fs; % Time Vector
NFFT = 2^nextpow2(L); % For Efficiency
FTData = fft(Data - mean(Data),NFFT)/L; % Fopurier Transform
Fv = linspace(0, 1, NFFT/2-1)*Fn; % Frequency Vector (For Plots)
Iv = 1:numel(Fv); % Index Vector (For Plots)
figure
% sp = [1:2:NrSp 2:2:NrSp]; % Order 'subplot' Plots
for k = 1:3
subplot(3,1,k)
plot(Fv, abs(FTData(Iv,k))*2) % Plot Fourier Transforms
grid
xlabel('Frequency')
ylabel('Magnitude')
xlim([0 Fn/20])
% xlim([5 10])
ylim([0 0.4])
title(sprintf('Column %d',k))
end
sgtitle('Fourier Transform of Data')
figure
% sp = [1:2:NrSp 2:2:NrSp];
for k = 1:NrSp
subplot(3,1,k)
plot(t, Data(:,k))
grid
ylim([-3 3])
xlabel('Time')
ylabel('Amplitude')
title(sprintf('Column %d',k))
end
sgtitle('Original Time-Domain Data')
Wp = [0.0001 0.0075]/Fn; % Define Passband In Hz, Normalise To (0,pi)
Ws = Wp.*[0.95 1.05]; % Stopband (Normalised)
Rs = 50; % Stopband Ripple (Attenuation) dB
Rp = 1; % Passband Ripple dB
[n,Wn] = ellipord(Wp,Ws,Rp,Rs); % Calculate Filter Order
[z,p,k] = ellip(n,Rp,Rs,Wp); % Design Filter
[sos,g] = zp2sos(z,p,k); % Implement Filter As Second-Order-Section Representation
figure
freqz(sos, 2^16, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 0.01])
set(subplot(2,1,2), 'XLim',[0 0.01])
sgtitle('Filter Bode Plot')
Data_Filt = filtfilt(sos,g,Data);
figure
for k = 1:3
subplot(3,1,k)
plot(t, Data_Filt(:,k))
grid
ylim([-1 1]*0.75)
xlabel('Time')
ylabel('Amplitude')
title(sprintf('Column %d',k))
end
sgtitle('Time-Domain Plots of Bandpass (1E-4 - 75E-4 Hz) Filtered Data')
Adjust the upper passband (the second value in ‘Wp’, 0.0075 Hz in this example) to get the filtered result you want. Use the fft result to guide that choice. Use the same filter for all the signals.
.
Paras Shaikh
on 27 Sep 2022
Star Strider
on 27 Sep 2022
Edited: Star Strider
on 1 Oct 2022
With a sampling frequency of 1 Hz, as you specified in your earlier Comment, the highest frequency you can even see in this signal is 0.5 Hz (the Nyquist frequency).
However if you meant that each column was 4500 samples in one second, then that makes the sampling frequency is 4500 Hz.
There is not much information in the filtered signal —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1124040/labeled%20final%20data.csv');
Acc = T1{:,1:3};
Gyr = T1{:,4:6};
Data = Acc;
Fs = 4500; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
NrSp = size(Data,2); % Number Of Subplots
L = size(Data,1); % Length Of Data Vectors
t = linspace(0, Fs, L)/Fs; % Time Vector
NFFT = 2^nextpow2(L); % For Efficiency
FTData = fft(Data - mean(Data),NFFT)/L; % Fourier Transform
Fv = linspace(0, 1, NFFT/2-1)*Fn; % Frequency Vector (For Plots)
Iv = 1:numel(Fv); % Index Vector (For Plots)
figure
% sp = [1:2:NrSp 2:2:NrSp]; % Order 'subplot' Plots
for k = 1:3
subplot(3,1,k)
plot(Fv, abs(FTData(Iv,k))*2) % Plot Fourier Transforms
grid
xlabel('Frequency')
ylabel('Magnitude')
xlim([0 Fn/200])
% xlim([5 10])
ylim([0 0.4])
title(sprintf('Column %d',k))
end
sgtitle('Fourier Transform of Data')
figure
% sp = [1:2:NrSp 2:2:NrSp];
for k = 1:NrSp
subplot(3,1,k)
plot(t, Data(:,k))
grid
ylim([-3 3])
xlabel('Time')
ylabel('Amplitude')
title(sprintf('Column %d',k))
end
sgtitle('Original Time-Domain Data')
Wp = [2 8]/Fn; % Define Passband In Hz, Normalise To (0,pi)
Ws = Wp.*[0.95 1.04]; % Stopband (Normalised)
Rs = 50; % Stopband Ripple (Attenuation) dB
Rp = 1; % Passband Ripple dB
[n,Wn] = ellipord(Wp,Ws,Rp,Rs); % Calculate Filter Order
[z,p,k] = ellip(n,Rp,Rs,Wp); % Design Filter
[sos,g] = zp2sos(z,p,k); % Implement Filter As Second-Order-Section Representation
figure
freqz(sos, 2^16, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 10])
set(subplot(2,1,2), 'XLim',[0 10])
sgtitle('Filter Bode Plot')
Data_Filt = filtfilt(sos,g,Data);
figure
for k = 1:3
subplot(3,1,k)
plot(t, Data_Filt(:,k))
grid
ylim([-1 1]*0.75)
xlabel('Time')
ylabel('Amplitude')
title(sprintf('Column %d',k))
end
sgtitle(sprintf('Time-Domain Plots of Bandpass (%.1f - %.1f Hz) Filtered Data',Wp*Fn))
FTData_Filt = fft(Data_Filt - mean(Data_Filt),NFFT)/L; % Fourier Transform
figure
% sp = [1:2:NrSp 2:2:NrSp]; % Order 'subplot' Plots
for k = 1:3
subplot(3,1,k)
plot(Fv, abs(FTData_Filt(Iv,k))*2) % Plot Fourier Transforms
grid
xlabel('Frequency')
ylabel('Magnitude')
xlim([0 Fn/200])
% xlim([5 10])
ylim([0 0.4])
title(sprintf('Column %d',k))
end
sgtitle('Fourier Transform of Filtered Data')
The filter is operating correctly and the Fourier transforms of the filtered data are as expected.
EDIT — (1 Oct 2022 at 17:15)
Added this plot, as requested —
figure
hold on
for k = 1:3
plot(t, Data_Filt(:,k), 'DisplayName',sprintf('Column %2d',k))
end
hold off
grid
ylim([-1 1]*0.75)
xlabel('Time')
ylabel('Amplitude')
title(sprintf('Time-Domain Plots of Bandpass (%.1f - %.1f Hz) Filtered Data',Wp*Fn))
legend('Location','best')
.
Paras Shaikh
on 1 Oct 2022
Star Strider
on 1 Oct 2022
Of course!
Paras Shaikh
on 1 Oct 2022
Star Strider
on 1 Oct 2022
That is not the way your data are organised.
That plots all three accelometer axes over their shared time vector. The original data all go from 0 to about 1 second, because you have one second of data in the files provided. They are different axes of the acclerometer, all collected over the same time interval at the same instants in time. Putting them together sequentially would be meaningless.
Paras Shaikh
on 13 Oct 2022
Star Strider
on 13 Oct 2022
This one, and because it meets your requirements —
Fs = 4500; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Wp = [2 8]/Fn; % Define Passband In Hz, Normalise To (0,pi)
Ws = Wp.*[0.95 1.04]; % Stopband (Normalised)
Rs = 50; % Stopband Ripple (Attenuation) dB
Rp = 1; % Passband Ripple dB
[n,Wn] = ellipord(Wp,Ws,Rp,Rs); % Calculate Filter Order
[z,p,k] = ellip(n,Rp,Rs,Wp); % Design Filter
[sos,g] = zp2sos(z,p,k); % Implement Filter As Second-Order-Section Representation
figure
freqz(sos, 2^16, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',[0 10])
set(subplot(2,1,2), 'XLim',[0 10])
sgtitle('Filter Bode Plot')
Data_Filt = filtfilt(sos,g,Data);
.
Paras Shaikh
on 13 Oct 2022
Star Strider
on 13 Oct 2022
Categories
Find more on Frequency Transformations 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!












