Filtering high frequencies from response signal

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;

 Accepted 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.

5 Comments

My pleasure!
The ‘Wp’ value defines the maximum frequency the filter passes, and so is the cutoff (half-power) frrequency for the lowpass filter (the higher frequency, ‘Ws’, is the stopband frequency that between them define the transition region). The stopband frequency is automatically calculated from the passband frequency (in my code), so it is only necessary to change ‘Wp’ to change the filter behaviour. Experiment with it to get the output you want. Use the Fourier Transform plot to guide your decisions.
The highest frequency poossible for both of them is ‘Fn’, the Nyquist frequency. They both must be below that value.
Is there a way to adjust the Wp value so the code filters out every frequency not within a certain range? Thanks :)
It would be straightforward to change that code to a bandpass filter.
Change ‘Wp’ and ‘Ws’ respectively to:
Wp = [Flo Fhi]/Fn; % Passband Frequency (Normalised)
Ws = [0.9 1.1].*Wp; % Stopband Frequency (Normalised)
where ‘Flo’ and ‘Fhi’ are the frequencies that define the filter passband. All of them must be between (that is, not including) 0 Hz and the Nyquist frequency. AAgain, use the Fourier Transform plot to guide your choices.
I would not use the Savitzky-Golay filter on your signal, since while useful in many situations, is difficult to use as a frequency-selective filter with your signal. It is primarily useful to eliminate broadband noise, and your signal has frequency-limited noise that you can eliminate with a frequency-selective filter.
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (1)

hello
try also sgolayfilt
function y=sgolayfilt(x,order,framelen,weights,dim)
%SGOLAYFILT Savitzky-Golay Filtering.
% SGOLAYFILT(X,ORDER,FRAMELEN) smooths the signal X using a
% Savitzky-Golay (polynomial) smoothing filter. The polynomial order,
% ORDER, must be less than the frame length, FRAMELEN, and FRAMELEN must
% be odd. The length of the input X must be >= FRAMELEN. If X is a
% matrix, the filtering is done on the columns of X.

Products

Tags

Community Treasure Hunt

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

Start Hunting!