MATLAB: Filtering high frequencies from response signal

filterMATLAB

I have a data set comprising a number of strain measurements obtained from a strain gauge and would like to filter out the ultra high frequncy noise present inbetween the seperate measurements. See code attached below šŸ™‚
clc, clear, close all;
load('7004x4.mat')
t= g{:,1};
sm= g{:,3};
sm= rmmissing(sm);
t=rmmissing(t);
n=10;
t = arrayfun(@(i) mean(t(i:i+n-1)),1:n:length(t)-n+1)';
sm= arrayfun(@(i) mean(sm(i:i+n-1)),1:n:length(sm)-n+1)';
plot(t,sm)
xlabel('Time Elapsed (s)')
ylabel('Strain')
title('Strain Signal')
fs= 6.2e-4;

Best Answer

Try this:
D1 = load('7004x4.mat');
T1 = D1.g;
Q1 = T1(1:5,:);
t = T1{:,1};
sm = T1{:,3};
t = rmmissing(t);
sm = rmmissing(sm);
% Signal Vector
L = size(sm,1); % Data Length
Fs = 1/mean(diff(t)); % Sampling Frequency
Ts = 1/Fs; % Sampling Interval
Fn = Fs/2; % Nyquist Frequency
smc = sm - mean(sm); % Subtract Mean (Makes Other Peaks More Prominent)
FTsm = fft(smc)/L; % Normalised Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector (One-Sided Fourier Transform)
figure
plot(Fv, abs(FTsm(Iv))*2)
grid
xlim([0 50])
title('Fourier Transform')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
Wp = [30]/Fn; % Passband Frequency (Normalised)
Ws = [1.01].*Wp; % Stopband Frequency (Normalised)
Rp = 1; % Passband Ripple
Rs = 60; % Passband Ripple (Attenuation)
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Elliptic Order Calculation
[z,p,k] = ellip(n,Rp,Rs,Wp); % Elliptic Filter Design: Zero-Pole-Gain
[sos,g] = zp2sos(z,p,k); % Second-Order Section For Stability
figure
freqz(sos, 2^20, Fs) % Filter Bode Plot
set(subplot(2,1,1), 'XLim',Wp*Fn.*[0.8 1.2]) % Optional

set(subplot(2,1,2), 'XLim',Wp*Fn.*[0.8 1.2]) % Optional
sm_filtered = filtfilt(sos, g, sm); % Filter With IIR Filter
figure
plot(t, sm)
hold on
plot(t, sm_filtered)
hold off
grid
xlabel('Time (Units Estimated)')
ylabel('AMplitude (Units Not Specified)')
legend('Original Signal', 'Lowpass-Filtered Signal', 'Location','SE')
Adjust the value of ā€˜Wpā€™ (Passband Frequency) of the filter to get the result you want. The Fourier transform plot can help with that decision.