Why the filter has no output?

2 views (last 30 days)
Xizeng Feng
Xizeng Feng on 23 Dec 2024
Moved: Paul on 23 Dec 2024
I designed an analoge filter, but it has no signal's output. Can anyone tell me what's wrong in my code?
t=0:.00001:1;
f1=40;f2=300;
wp=400;ws=1000;
x=2*sin(2*pi*f1*t)+0.5*sin(2*pi*f2*t); % signal
[n,wc]=buttord(wp,ws,1,40,'s'); % order and cutoff frequency of the filter
[b,a]=butter(n,wc,'s'); % the parameter of the filter
y=filter(b,a,x); %filtering the signal
subplot(2,1,1);
plot(t,x); % orginal signal
title('the orginal signal');
subplot(2,1,2);
plot(t,y); % filtered signal
title('the filtered signal')

Answers (4)

Paul
Paul on 23 Dec 2024
Edited: Paul on 23 Dec 2024
The function filter is only applicable for a discrete-time filter, not an analog filter. If you want to simulate the output from an analog filter you'll need a different function, something like lsim from the Control System Toolbox. Also, consider using one of the other forms of butter; transfer functions are typically not preferred due to numerical issues (though it may be o.k. in this instance, I didn't check).

Star Strider
Star Strider on 23 Dec 2024
You are designing a continuous filter. All the relevant functions (specifically the filter functions) in the Signal Processing Toolbox work only with discrete (digital) filters.
t=0:0.00001:1;
Fs = 1/t(2)
Fs = 1.0000e+05
f1=40;f2=300;
wp=400;ws=1000;
x=2*sin(2*pi*f1*t)+0.5*sin(2*pi*f2*t); % signal
[n,wc]=buttord(wp,ws,1,40,'s'); % order and cutoff frequency of the filter
[b,a]=butter(n,wc,'s'); % the parameter of the filter
figure
freqs(b,a)
y=filter(b,a,x); %filtering the signal <— Not actually
subplot(2,1,1);
plot(t,x); % orginal signal
title('the orginal signal');
subplot(2,1,2);
plot(t,y); % filtered signal
% ------------------ Design Discrete (Digital) Version Of This Filter ------------------
wp=400;ws=1000;
x=2*sin(2*pi*f1*t)+0.5*sin(2*pi*f2*t); % signal
[n,wc]=buttord(2*wp/Fs,2*ws/Fs,1,40); % order and cutoff frequency of the filter
[b,a]=butter(n,wc); % the parameter of the filter
figure
freqz(b, a, 2^16, Fs)
set(subplot(2,1,1), 'YLim', [-5E+2 10], 'XScale','log')
set(subplot(2,1,2), 'XScale','log')
y=filtfilt(b,a,x); %filtering the signal <— Use ‘filtfilt’
subplot(2,1,1);
plot(t,x); % orginal signal
title('the orginal signal');
subplot(2,1,2);
plot(t,y); % filtered signal
Creating a discrete (digital) filter with those passband and stopband frequencies works, however since the signal frequencies are well inside the passband, there is no attenuation.
.

Xizeng Feng
Xizeng Feng on 23 Dec 2024
According some books, for exmple the "MATLAB Applications for Digital Signal Processing
from Sampling to Filter Design " By Orhan Gazi, analog filter can be designed by Matlab functions. Of course, the parameters should include the 's' . When I wrote the codes , that book gave me some exsamples.
  6 Comments
Star Strider
Star Strider on 23 Dec 2024
You cannot use annalog (continuous) filters with sampled signals. You would have to realise continuous filters in analog hardware. Calculating the component values from the transfer function is not trivial, and it may not be possible. (The transfer function actually has to be improper — the numerator polynomial of higher degree than the denominator polynomial — for it to work.)
Paul
Paul on 23 Dec 2024
Moved: Paul on 23 Dec 2024
Running the code gives the result from the text.
f1=5;
f2=60;
t=-0.25:0.00125:0.25;
xc_t=cos(2*pi*f1*t)+cos(2*pi*f2*t);
% plot(t,xc_t);
% xlabel('t');
% ylabel('x_c(t)');
Ts=1/256;
ts=-0.25:Ts:0.25;
xn=cos(2*pi*f1*ts)+cos(2*pi*f2*ts);
Rp=2;
Rs=40;
wp=2*pi*10;
ws=2*pi*50;
This line computes the order and cutoff frequency for a continuous-time filter
[N, wc]=buttord(wp,ws,Rp,Rs,'s')
N = 4
wc = 99.3471
This line uses N to compute the transfer function of a discrete-time filter and normalizes wc to half the the sampling frequency.
[B, A]=butter(N,wc*Ts*1/pi);
Use filter because B(z)/A(z) is the transfer function of a discrete-time filter.
yn=filter(B,A,xn);
stem(ts,yn,'filled','r'), hold on;
plot(t,cos(2*pi*f1*t));
Yields the result from the text.
Not sure why the authors are using buttord for a continuous-time filter and then using those results to design a discrete-time filter. Maybe they were trying to illustrate a point for which we are missing some context.

Sign in to comment.


Xizeng Feng
Xizeng Feng on 23 Dec 2024
Here is another example of analog filter with Matlab codes:
fs = 100;
t = 0:1/fs:1;
x = sin(2*pi*t*3)+.25*sin(2*pi*t*40);
Now create a 6th-order Butterworth lowpass filter to filter out the high-frequency sinusoid. Filter x
using both filter and filtfilt for comparison:
[b,a] = butter(6,20/(fs/2));
y = filtfilt(b,a,x);
yy = filter(b,a,x);
plot(t,x,t,y,t,yy)
legend('Original','filtfilt','filter')
------------------------------------
I quoted them from the book "Signal Processing Toolbox User's Guide", page I-9. And the result is as following:

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!