Why the filter has no output?

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')

Antworten (4)

Paul
Paul am 23 Dez. 2024
Bearbeitet: Paul am 23 Dez. 2024

0 Stimmen

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 am 23 Dez. 2024

0 Stimmen

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 am 23 Dez. 2024

0 Stimmen

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 Kommentare

Sam Chak
Sam Chak am 23 Dez. 2024

Can you post the code snippets on the particular MATLAB functions used in the examples?

Xizeng Feng
Xizeng Feng am 23 Dez. 2024
yes, but a little bit later, I'll copy it and post here.
Xizeng Feng
Xizeng Feng am 23 Dez. 2024
Verschoben: Paul am 23 Dez. 2024
here are the codes of that book:
clc; clear all; close all;
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;
[N, wc]=buttord(wp,ws,Rp,Rs,'s')
[B, A]=butter(N,wc*Ts*1/pi); yn=filter(B,A,xn);
stem(ts,yn,'filled','r') hold on;
plot(t,cos(2*pi*f1*t));
Xizeng Feng
Xizeng Feng am 23 Dez. 2024
Verschoben: Paul am 23 Dez. 2024
it compares the analog filtering and digital filtering.
Star Strider
Star Strider am 23 Dez. 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 am 23 Dez. 2024
Verschoben: Paul am 23 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Xizeng Feng
Xizeng Feng am 23 Dez. 2024

0 Stimmen

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:

Produkte

Version

R2018b

Gefragt:

am 23 Dez. 2024

Verschoben:

am 23 Dez. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by