Error using ellipord?
古いコメントを表示
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
2023 年 5 月 19 日
0 投票
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
.
8 件のコメント
Zalupa
2023 年 5 月 22 日
Zalupa
2023 年 5 月 22 日
I made a few changes.
It now works —
% Recursive Filter
Ws=20; % set delay band frequencies
Wp=10; % set passband frequencies
Rs=80; % ripple (attenuation) in the delay band
Rp=2.0; % ripple in the passband
N_bit=14; % number of bits for representing the fractional part of coefficients
fd=120; % sampling frequency
f_N = fd/2; % Nyquist frequency
Fs_norm = Ws/f_N; % normalized delay band frequencies
Fp_norm = Wp/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; % output the filter order
fc = Wn * f_N; % output the cutoff frequencies
[z, p, k] = ellip(n, Rp, Rs, Wn, 'low'); % Compute the numerator and denominator coefficients of the filter
[sos, g] = zp2sos(z, p, k);
[b,a] = sos2tf(sos,g)
% [b, a] = ellip(n, Rp, Rs, Wn, 'stop'); % find the numerator and denominator coefficients of the filter
% b = b(:); % convert row vector to column vector
% a = a(:);
% b; % output the numerator coefficients
% a; % output the denominator coefficients
f = 0 : 1 : f_N; % define frequency vector for calculating frequency response
% h = freqz(b, a, f, fd); % calculate the complex transfer coefficient
h = freqz(sos, f, fd); % Compute the complex frequency response
figure(11); plot(f, abs(h)); grid on; % plot the frequency response
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); % calculate the complex transfer coefficient of the quantized filter
figure(12);
plot(f, abs(h), f, abs(hq)); grid on; % plot the frequency response of the filter before and after quantization
sos = tf2sos(b, a); % convert from direct form to second-order section form
sosq = round(2^N_bit * sos) / 2^N_bit; % quantize the coefficients of the biquad sections
[bs, as] = sos2tf(sos); % convert back to direct form representation
hs = freqz(bs, as, f, fd); % calculate the complex transfer coefficient
figure(13); plot(f, abs(hs)); grid on; % plot the frequency response
load Lab_2_1.mat % load the signal
% signal_f1 = filter(b, a, signal); % filter the signal
L = numel(signal);
t = linspace(0, L-1, L)/fd;
signal_f1 = filtfilt(sos, g, signal); % Filter the signal using the original filter coefficients
figure
tiledlayout(2,1)
nexttile
plot(t, signal)
grid
xlabel('Time')
ylabel('Amplitude')
title('Original')
nexttile
plot(t, signal_f1)
grid
xlabel('Time')
ylabel('Amplitude')
title('Filtered')
.
Zalupa
2023 年 5 月 22 日
移動済み: Star Strider
2023 年 5 月 22 日
Zalupa
2023 年 5 月 22 日
移動済み: Star Strider
2023 年 5 月 22 日
Star Strider
2023 年 5 月 22 日
Your code works. I made a few changes to it based on my expierience.
This is your assignment, so I will let you deal with getting it in an acceptable format. If you have questions about that, it would be best to discuss that with your instructor.
Zalupa
2023 年 5 月 22 日
Star Strider
2023 年 5 月 22 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
カテゴリ
ヘルプ センター および File Exchange で Analog Filters についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



