I have a problem with butter filter and I can’t fix it

조회 수: 6 (최근 30일)
Melika Eft
Melika Eft 2023년 3월 14일
편집: Walter Roberson 2023년 3월 14일
Hello I just wrote this code and I have a problem with line which I used butter filter thank you
% Define the bandpass signal (sine wave)
fs = 1000; % sampling frequency
t = 0:1/fs:1-1/fs; % time vector
f1 = 100; % signal frequency
f2 = 300; % signal frequency
x = sin(2*pi*f1*t) + sin(2*pi*f2*t); % bandpass signal
% Obtain the Fourier transform of the bandpass signal
X = fft(x);
% Define the cutoff frequency for the lowpass filter
fc = 500; % cutoff frequency
% Create a Butterworth lowpass filter
N = 4; % filter orde
[b,a] = butter(N, fc/(fs/2));
% Apply the filter to the Fourier transform of the signal
Y = filter(b, a, X);
% Obtain the time-domain representation of the filtered signal
y = real(ifft(Y));
% Plot the original and filtered signals
figure;
subplot(2,1,1);
plot(t,x);
title('Bandpass Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(t,y);
title('Lowpass Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');

채택된 답변

Les Beckham
Les Beckham 2023년 3월 14일
The error was because you were trying to set the cutoff frequency at the Nyquist frequency (half the sample rate).
The error message is pretty clear.
Below is modified to format your code properly and adjust the cutoff frequency to a valid value. You can adjust to get the behavior you are looking for.
% Define the bandpass signal (sine wave)
fs = 1000; % sampling frequency
t = 0:1/fs:1-1/fs; % time vector
f1 = 100; % signal frequency
f2 = 300; % signal frequency
x = sin(2*pi*f1*t) + sin(2*pi*f2*t); % bandpass signal
% Obtain the Fourier transform of the bandpass signal
X = fft(x);
% Define the cutoff frequency for the lowpass filter
fc = 400; %500; % cutoff frequency <<< You can't set a cutoff at the Nyquist frequency (half the sample rate)
% Create a Butterworth lowpass filter
N = 4; % filter order
fc/(fs/2) % <<< valid (less than 1)
ans = 0.8000
[b,a] = butter(N, fc/(fs/2));
% Apply the filter to the Fourier transform of the signal
Y = filter(b, a, X);
% Obtain the time-domain representation of the filtered signal
y = real(ifft(Y));
% Plot the original and filtered signals
figure;
subplot(2,1,1);
plot(t,x);
title('Bandpass Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(t,y);
title('Lowpass Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');
  댓글 수: 3
Rik
Rik 2023년 3월 14일
Next time, please format the code as code yourself.
If this answer solved your question, please consider marking it as accepted answer and/or giving it an upvote.
Les Beckham
Les Beckham 2023년 3월 14일
You are quite welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Digital Filter Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by