Question

let say we have following signal

function [ x ] = generate1(N,m,A3)
f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))'; %'
wn = randn(length(t),1); %zero mean variance 1
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;
%[pks,locs] = findpeaks(x);
 %plot(x)
end

with generation code

y=generate1(3,500,1);

now suppose that i want all components which are with frequency below then 150,so i need low pass filter,but how to apply in this case?please tell me,i am new in filter design problems,can i construct filter myself?thanks in advance

Was it helpful?

Solution

Something like this should work:

function [ x ] = generate1(N,m,A3)
f1 = 100;
f2 = 200;
T = 1./f1;
t = (0:(N*T/m):(N*T))'; %'
fs = m/(T*N);
cutoff = 150;


wn = randn(length(t),1); %zero mean variance 1
x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;

[b,a] = butter(4, cutoff/(fs/2));

x_filt = filtfilt(b,a,x);
figure; plot(t,x)
hold on
plot(t,x_filt)
%[pks,locs] = findpeaks(x);
%plot(x)
end

The call to the butter function constructs a 4th order butterworth lowpass filter with a cutoff frequency specified by the cutoff parameter above (In our example it is 150Hz). the "a" and "b" values are the filter coefficients, which are applied to the x vector with the filtfilt() command. the result "x_filt" is a filtered version of x which contains frequency components less than 150Hz.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top