Error using ellipord?

9 ビュー (過去 30 日間)
Zalupa
Zalupa 2023 年 5 月 19 日
コメント済み: Star Strider 2023 年 5 月 22 日
Fs = [3000 8000]; % Define the stopband frequencies (Hz)
Fp = [2500 8500]; % Define the passband frequencies (Hz)
Rs = 80; % Stopband attenuation (dB)
Rp = 1.3; % Passband ripple (dB)
N_bit = 14; % Number of bits for coefficient representation
fd = 48000; % Sampling frequency (Hz)
f_N = fd/2; % Nyquist frequency (Hz)
Fs_norm = Fs/f_N; % Normalized stopband frequencies
Fp_norm = Fp/f_N; % Normalized passband frequencies
[n, Wn] = ellipord(Fp_norm, Fs_norm, Rp, Rs); % Determine the minimum filter order 'n' and cutoff frequencies 'Wn'
n % Display the filter order
fc = Wn * f_N % Display the cutoff frequencies
[b, a] = ellip(n, Rp, Rs, Wn, 'stop'); % Compute the numerator and denominator coefficients of the filter
b = b(:); % Convert the numerator coefficient vector from row to column
a = a(:); % Convert the denominator coefficient vector from row to column
b % Display the numerator coefficients
a % Display the denominator coefficients
f = 0 : 1 : f_N; % Define the frequency vector for computing the frequency response
h = freqz(b, a, f, fd); % Compute the complex frequency response
figure(11);
plot(f, abs(h));
grid on;
title('Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
legend('Original Filter');
bq = round(2^N_bit * b) / 2^N_bit; % Quantize the numerator coefficients
aq = round(2^N_bit * a) / 2^N_bit; % Quantize the denominator coefficients
hq = freqz(bq, aq, f, fd); % Compute the frequency response of the quantized filter
figure(12);
plot(f, abs(h), f, abs(hq));
grid on;
title('Frequency Response (Original vs Quantized)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
legend('Original Filter', 'Quantized Filter');
sos = tf2sos(b, a); % Convert to second-order sections (SOS) form
sosq = round(2^N_bit * sos) / 2^N_bit; % Quantize the coefficients of the second-order sections
[bs, as] = sos2tf(sosq); % Convert back to the numerator-denominator representation
hs = freqz(bs, as, f, fd); % Compute the frequency response of the quantized filter in SOS form
figure(13);
plot(f, abs(hs));
grid on;
title('Frequency Response (Quantized Filter in SOS Form)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
load Lab_2 % Load the signal
signal_f1 = filter(b, a, signal); % Filter the signal using the original filter coefficients
I get this error
Error using ellipord
The frequency vectors must both be the same length.
Error in untitled3 (line 11)
[n, Wn] = ellipord(Fp_norm, Fs_norm, Rp, Rs); % визначаємо мінімальний порядок фільтра
how to fix it ?

回答 (1 件)

Star Strider
Star Strider 2023 年 5 月 19 日
The code appears to work correctly when I run it (in R2023a), however I don’t have the signal so the load call throws an error.
The transfer function realisation may cause problems, I would instead use:
[z, p, k] = ellip(n, Rp, Rs, Wn, 'stop'); % Compute the numerator and denominator coefficients of the filter
[sos, g] = zp2sos(z, p, k);
This is much more robust than using tf2sos. Make appropriate changes to the freqz and other calls to accommodate this change.
The freqz call would then be:
h = freqz(sos, f, fd); % Compute the complex frequency response
Then to do the actual filtering:
signal_f1 = filtfilt(sos, g, signal); % Filter the signal using the original filter coefficients
The filtfilt function is phase-neutral and produces no phase distortion in the filtered signal.
.
  8 件のコメント
Zalupa
Zalupa 2023 年 5 月 22 日
thank for help
Star Strider
Star Strider 2023 年 5 月 22 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnalog Filters についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by