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
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
The filtfilt function is phase-neutral and produces no phase distortion in the filtered signal.
.

8 件のコメント

Zalupa
Zalupa 2023 年 5 月 22 日
this is the file I'm downloading, can you please download the full code so I can check if everything is fine
Zalupa
Zalupa 2023 年 5 月 22 日
because,I can't run this code
% Recursive Filter
Fs=20; % set delay band frequencies
Fp=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 = Fs/f_N; % normalized delay band 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; % output the filter order
fc = Wn * f_N; % output the cutoff frequencies
[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
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
Star Strider
Star Strider 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 = 1×7
1.0e-03 * 0.4407 -0.5862 0.9982 -0.7230 0.9982 -0.5862 0.4407
a = 1×7
1.0000 -5.2210 11.7464 -14.5388 10.4268 -4.1060 0.6938
% [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
Zalupa 2023 年 5 月 22 日
移動済み: Star Strider 2023 年 5 月 22 日
Calculate the minimum necessary order of the indicated type of filter to meet the set requirements. Find the filter coefficients. Construct the frequency response of the calculated filter. To evaluate the effect of N-bit quantization of filter coefficients on its characteristics in direct implementation and cascade based on biquadratic links. Filter the specified signal stored in the Lab_2_1.mat file in the signal variable.
I have such a task, and I don't know if it will be correct
Zalupa
Zalupa 2023 年 5 月 22 日
移動済み: Star Strider 2023 年 5 月 22 日
it all needs to be combined into one code?
Star Strider
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
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!
.

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

タグ

質問済み:

2023 年 5 月 19 日

コメント済み:

2023 年 5 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by