Designing High Pass Filter

14 次查看(过去 30 天)
Zaref Li
Zaref Li 2024-1-6
编辑: Hassaan 2024-1-6
Hi everyone,
I want to design high pass filter, cut off freq=0,65pi. I wrote this code but it did not work. Can you help me?
clear all
clc
n = 1:15;
x = cos(2*pi*0.2*n);
xInterp = linspace(min(n), max(n), 5 * length(n)); %by a factor of 5
x_interpolated =interp1(n, x, xInterp); %Interpolate by a factor of 5 using zero padding
figure;
subplot(2,1,1);
stem(n, x);
title('Original Signal');
subplot(2,1,2);
stem(1:length(x_interpolated), x_interpolated);
title('Interpolated Signal');
Fc = 0.65*pi;
Fs = 15;
[xafiltfilt,hpdf] = highpass(x, Fc, Fs, 'ImpulseResponse','iir');
figure
freqz(hpdf.Coefficients, 2^16, Fs)
set(subplot(2,1,1), 'XLim',[0 10])
set(subplot(2,1,2), 'XLim',[0 10])

回答(1 个)

Hassaan
Hassaan 2024-1-6
编辑:Hassaan 2024-1-6
highpass function is designed to filter the signal, not to return the filter itself. You might be looking to use designfilt to create a filter object first and then apply it to the signal with filtfilt or another filtering function.
freqz function expects filter coefficients as inputs if you're not using a digital filter object.
Your current Fc and Fs values suggest that you are working with normalized frequency, but highpass expects the cutoff frequency in Hz. Since you're working with a sampled signal, your sampling frequency Fs should be higher than twice the highest frequency in your signal to satisfy the Nyquist criterion.
XLim property should be set on axes, not on subplots directly.
Approach 1
clear all
clc
% Signal generation
n = 1:15;
x = cos(2*pi*0.2*n);
% Interpolation (zero padding in frequency domain)
xInterp = linspace(min(n), max(n), 5 * length(n));
x_interpolated = interp1(n, x, xInterp, 'linear', 'extrap');
% Plotting the original and interpolated signals
figure;
subplot(2,1,1);
stem(n, x);
title('Original Signal');
subplot(2,1,2);
stem(xInterp, x_interpolated);
title('Interpolated Signal');
% Filter design
Fc = 0.65; % Normalized cutoff frequency (0.65 * Nyquist frequency)
Fs = 5 * length(n); % Sampling frequency after interpolation
% Filter design with lower order
hpdf = designfilt('highpassfir', 'FilterOrder', 20, ...
'CutoffFrequency', Fc, 'SampleRate', Fs);
% Verify if the signal length is sufficient
if length(x_interpolated) > 3 * (length(hpdf.Coefficients) - 1)
xafiltfilt = filtfilt(hpdf, x_interpolated);
else
error('Data length is not sufficient for the designed filter order.');
end
% Apply filter to the signal
xafiltfilt = filtfilt(hpdf, x_interpolated);
% Frequency response of the filter
figure
[fresp, freq] = freqz(hpdf, 2^16, Fs);
subplot(2,1,1);
plot(freq/pi, abs(fresp));
title('Magnitude Response');
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Magnitude');
xlim([0 1]); % Normalized frequency x-axis limit
subplot(2,1,2);
plot(freq/pi, angle(fresp));
title('Phase Response');
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Phase (radians)');
xlim([0 1]); % Normalized frequency x-axis limit
The designfilt function creates a digital filter, which is then applied to the interpolated signal using filtfilt. The freqz function analyzes the frequency response of the designed filter. The xlim function sets the x-axis limits correctly for the plots.
Approach 2
clear all
clc
% Signal generation
n = 1:15;
x = cos(2*pi*0.2*n);
% Interpolation
xInterp = linspace(min(n), max(n), 5 * length(n));
x_interpolated = interp1(n, x, xInterp, 'linear', 'extrap');
% Plotting the original and interpolated signals
figure;
subplot(2,1,1);
stem(n, x);
title('Original Signal');
subplot(2,1,2);
stem(xInterp, x_interpolated);
title('Interpolated Signal');
% Filter design
Fc = 0.65; % Normalized cutoff frequency (0.65 * Nyquist frequency)
Fs = 5 * length(n); % Sampling frequency after interpolation
% Designing a high-pass filter with a lower order
hpdf = designfilt('highpassfir', 'FilterOrder', 20, ...
'CutoffFrequency', Fc, 'SampleRate', Fs);
% Apply filter to the signal
% Pad the signal to meet the data length requirement for filtfilt
pad_length = 3 * (length(hpdf.Coefficients) - 1);
x_padded = [zeros(pad_length, 1); x_interpolated(:); zeros(pad_length, 1)];
% Apply filter to the padded signal
xafiltfilt_padded = filtfilt(hpdf, x_padded);
% Remove padding
xafiltfilt = xafiltfilt_padded(pad_length+1:end-pad_length);
% Plot the filtered signal
figure;
stem(xInterp, xafiltfilt);
title('Filtered Signal');
% Frequency response of the filter
figure
[fresp, freq] = freqz(hpdf, 2^16, Fs);
subplot(2,1,1);
plot(freq/pi, abs(fresp));
title('Magnitude Response');
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Magnitude');
xlim([0 1]); % Normalized frequency x-axis limit
subplot(2,1,2);
plot(freq/pi, angle(fresp));
title('Phase Response');
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Phase (radians)');
xlim([0 1]); % Normalized frequency x-axis limit
The signal is padded, filtered, and the padding is removed. Lastly, the frequency response of the filter is plotted.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering

Community Treasure Hunt

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

Start Hunting!

Translated by