MATLAB real-time filtering implement

52 views (last 30 days)
How can implement a real-time filter in matlab. My goal is to design a filter with 100Hz cutoff frequency and 1dB attenuation in passband. My incoming signal has the sample rate of 10kHz. The signal is coming frame by frame. One frame contain 60 sample.
I tried following code but the result is bad. I kind know the problem is I filter the data every frame, but I don't know how to address that since it will be a real-time system and I have to process data frame by frame.
clear all
% Test Seq generate %
T = 9*(1/50);
fs = 10000;
t = 0:1/fs:T-1/fs;
testSeqComb = 3 * (sin(2*pi*50*t) + 0.5*sin(2*pi*500*t) + 0.25*sin(2*pi*1000*t));
blockNum = length(testSeqComb)/60;
for i=1:blockNum
testSeq(i,:) = testSeqComb(1+(i-1)*60:i*60);
end
% Raw Data Visualize %
plot(t, testSeqComb)
hold on
% Filter %
filter1 = designfilt('lowpassiir', 'FilterOrder', 6, 'HalfPowerFrequency', 100, 'SampleRate', 10000);
for i=1:blockNum
testSeqFiltered(i,:) = filtfilt(filter1, testSeq(i,:));
end
testSeqFilteredComb = [];
for i=1:blockNum
testSeqFilteredComb = [testSeqFilteredComb testSeqFiltered(i,:)];
end
% Filtered Data Visualize %
plot(t, testSeqFilteredComb, "LineWidth", 2)

Accepted Answer

Walter Roberson
Walter Roberson on 24 Feb 2022
60 cycles of 10 kHz is at least 10000/(60/2) = 333 Hz separation. You cannot filter down as far as 100 Hz with that.
For 100 Hz resolving power you would need at least 2/100 seconds of data, but you only have 0.3 of that 2/100 seconds .
You would need 200 samples of 10 kHz to be able to resolve 100 Hz.
Fs = 10000;
Hz = 100;
N = 60;
t = (0:N-1)/Fs;
x = sin(2*pi*Hz*t);
filter1 = designfilt('lowpassiir', 'FilterOrder', 6, 'HalfPowerFrequency', 100, 'SampleRate', 10000);
filtered = filtfilt(filter1, x);
plot(t, x, 'b', t, filtered, 'r');
legend({'original', 'filtered'})
Not no effect, but nothing too useful.
And since you want lowpass, then you need a lot more samples to be able to resolve those frequencies. The duration at a given sampling frequency controls not only the upper frequency that can be resolved, but also the lowest frequency that can be resolved.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!