为了能到远方,脚下的每一步都不能少.|

LZHMS

园龄:2年6个月粉丝:1关注:7

2023-10-30 19:47阅读: 8评论: 0推荐: 0

Signal Filters Design Based on Digital Signal Processing

Thoeries

I. Fourier Series Expansion Algorithm

We can utilize the Fourier Series to produce the analog signal with some frequency components. For any signal, its Fourier series expansion is defined as

x(t)=A02+n=1Ancos(nΩt+φn)

In the equation,A02 represents the DC component, A1cos(Ωt+φ1), represents the fundamental component of the signal, Ancos(nΩt+φn) represents the nth harmonic component of the signal. Moreover, analog angular frequency Ω=2πT=2πf.
Therefore, in this project we select three different frequency components, that is f1,f2,f3, to synthesize the final required analog signal:

x(t)=A02+A1cos(2πf1t+φ1)+A2cos(2×2πf2t+φ2)+A3cos(3×2πf3t+φ3)

For simplicity, there we respectively select these values:

A0=0,A1=1,A2=1,A3=1
φ1=φ2=φ3=0 Following above expression, we can get the generated analog signal: x(t)=cos(2πf1t)+cos(4πf2t)+cos(6πf3t)

II. Sample the Analog Signal

Time Domain Sampling Theorem

According to the time domain sampling theorem, the sampling frequency must be greater than twice the signal cutoff frequency.
Let's assume that the sampling frequency is Fs, and the generated analog signal frequency satisfies: F1<F2<F3, so the signal cutoff frequency is Fc=F3. The sampling theorem is formally expressed as:

Fs>2Fc

In this experiment,we respectively selected F1=10Hz,F2=20Hz,F3=30Hz to produce analog signal. So we can get the period and cutoff frequency of sampled signal:

Tc=1F1=0.1s,Fc=F3=30Hz

Time-domain Window

For periodic continuous signals, we intercept at integer multiples of the period to obtain a sequence for spectrum analysis.

Tp=NTc,NZ+

Sampling Frequency

For a specific sampling frequency, we can get the sampling period Ts, and the number of sampling points N:

Ts=1Fs,N=TpFc

Therefore, we use sampling frequency of Fs=90Hz,Fs=60Hz,Fs=40Hz to get time-domain signals.

Spectral Resolution

Spectral resolution is defined as the minimum separation between two signals of different frequencies:

Δf=FsN=1NTs=1Tp

III. Spectral Analysis

In this section, we will analyse the Amplitude-Frequency Characteristics and Phase-Frequency Characteristics of the sampled signal.

Convert to Frequency

When analysing the spectral, we need to convert the 0N1 to frequency sequence:

fk=kFsN,k=0,1,...N1

Convert to Real Amplitude

After we apply Discrete Fourier Transform to the sampled signal, the frequency-domain signal is complex-valued. And due to the time-domain signal is real-valued, the the frequency-domain signal is conjugate symmetric:

X(k)=X(Nk),k=0,1,...N1

For complex values, that means its real part is even symmetric about the middle point, and its imaginary part is odd symmetric about the middle point. This will be showed in the following figures.

Experiments

Experiment I: Tp=3Tc,Fs=90Hz

  • Samping Frequency Fs=3Fc(Fs>2Fc)
    We use the sampling frequency of Fs=90Hz under the condition of Tp=3Tc.
    Sampled Signal
    Spectral Analysis Graph
  • Conclusions
    The sampling frequency satisfies the Time Domain Sampling Theorem so we can see there is no overlap in frequency domain about the amplitude-frequency characteristic. And when f=10Hz, f=20Hz, f=30Hz, we can get the amplitude very close to 1 which is us defined in analop signal.

Experiment II: Tp=3Tc,Fs=60Hz

  • Samping Frequency Fs=2Fc(Fs=2Fc)
    We use the sampling frequency of Fs=60Hz under the condition of Tp=3Tc.
    Sampled Signal
    Spectral Analysis Graph
  • Conclusions
    The sampling frequency equals the threhold of Time Domain Sampling Theorem so we can easily see that it will just become overlapping in frequency domain. And when f=30Hz that is also Fs/2 point, we can get this point very close to its symmetric frequency point.

Experiment III: Tp=3Tc,Fs=40Hz

  • Samping Frequency Fs=43Fc(Fs<2Fc)
    We use the sampling frequency of Fs=40Hz under the condition of Tp=3Tc.
    Sampled Signal
    Spectral Analysis Graph
  • Conclusions
    The sampling frequency do not equal the Time Domain Sampling Theorem so we can obviously see that it has discarded the third frequency f=30Hz, which is caused by overlapping in frequency domain.

Note: in order to clearly analyse spectral of sampled signal, we also select the Time-domain Window of Tp=50Tc to conduct experiments.

Results

Experiment I: Fs=40Hz

Codes

%% Project Introduction:
%   This project is developed to design some signal filters based on digital
% signal processing.
clear, close all;
%% Produce and sample digital signal
f1 = 10;
f2 = 20;
f3 = 30;    % so the fc = f3 = 30Hz
Np = 50;          % number of periods for time-domain window
%% Experiment 1 (Choosing samling frequency fs = 3fc (fs > 2fs))
fs1 = 90;          % sampling frequency
xn1 = ProduceSamplingSignal(f1, f2, f3, fs1, Np, 'Sampling Analog Signal(fs = 3fc)');
DFTAnalysis(xn1, fs1, 'Frequency Response Characteristics(fs = 3fc)');

%% Experiment 2 (Choosing samling frequency fs = 2fc)
fs2 = 60;          % sampling frequency
xn2 = ProduceSamplingSignal(f1, f2, f3, fs2, Np, 'Sampling Analog Signal(fs = 2fc)');
DFTAnalysis(xn2, fs2, 'Frequency Response Characteristics(fs = 2fc)');

%% Experiment 3 (Choosing samling frequency fs < 2fc)
fs3 = 40;          % sampling frequency
xn3 = ProduceSamplingSignal(f1, f2, f3, fs3, Np, 'Sampling Analog Signal(fs < 2fc)');
DFTAnalysis(xn3, fs3, 'Frequency Response Characteristics(fs < 2fc)');

%% Experiment Description
% Experiment 4-7: Design a digital filter respectively with band pass, high
% pass, low pass, band stop based on ellipord.
%% Experiment 4: Design a digital filter with band pass using ellipord
fpl = 15; fpu=25; fsl=13; fsu=28;
rp = 1;             
rs = 40;
ellipBandPass(fpl, fpu, fsl, fsu, rp, rs, xn1, fs1, f1, Np, 'Digital Filter With Band Pass Using Ellipord(fs = 3fc)');
ellipBandPass(fpl, fpu, fsl, fsu, rp, rs, xn2, fs2, f1, Np, 'Digital Filter With Band Pass Using Ellipord(fs = 2fc)');
ellipBandPass(8, 10, 6, 12, rp, rs, xn3, fs3, f1, Np, 'Digital Filter With Band Pass Using Ellipord(fs < 2fc)');

%% Experiment 5: Design a digital filter with high pass using ellipord
fpz = 16; fsz = 13;
rp = 1;             
rs = 40;
ellipHighPass(fpz, fsz, rp, rs, xn1, fs1, f1, Np, 'Digital Filter With High Pass Using Ellipord(fs = 3fc)');
ellipHighPass(fpz, fsz, rp, rs, xn2, fs2, f1, Np, 'Digital Filter With High Pass Using Ellipord(fs = 2fc)');
ellipHighPass(15, 12, rp, rs, xn3, fs3, f1, Np, 'Digital Filter With High Pass Using Ellipord(fs < 2fc)');

%% Experiment 6: Design a digital filter with low pass using ellipord
fpz = 23; fsz=28; 
rp = 1;             
rs = 40;
ellipLowPass(fpz, fsz, rp, rs, xn1, fs1, f1, Np, 'Digital Filter With Low Pass Using Ellipord(fs = 3fc)');
ellipLowPass(fpz, fsz, rp, rs, xn2, fs2, f1, Np, 'Digital Filter With Low Pass Using Ellipord(fs = 2fc)');
ellipLowPass(12, 15, rp, rs, xn3, fs3, f1, Np, 'Digital Filter With Low Pass Using Ellipord(fs < 2fc)');

%% Experiment 7: Design a digital filter with band stop using ellipord
fpl = 15; fpu=25; fsl=17; fsu=22;
rp = 1;             
rs = 40;
ellipBandStop(fpl, fpu, fsl, fsu, rp, rs, xn1, fs1, f1, Np, 'Digital Filter With Band Stop Using Ellipord(fs = 3fc)');
ellipBandStop(fpl, fpu, fsl, fsu, rp, rs, xn2, fs2, f1, Np, 'Digital Filter With Band Stop Using Ellipord(fs = 2fc)');
ellipBandStop(5, 17, 8, 12, rp, rs, xn3, fs3, f1, Np, 'Digital Filter With Band Stop Using Ellipord(fs < 2fc)');

%% Experiment Description
% Experiment 8-11: Design a digital filter respectively with high pass, low
% pass, band pass, band stop based on hamming window.
%% Experiment 8: Design a digital filter with high pass using hamming window
fpz = 16; fsz = 13;
firlHighPass(fpz, fsz, xn1, fs1, f1, Np, 'Digital Filter With High Pass Using Hamming Window(fs = 3fc)');
firlHighPass(fpz, fsz, xn2, fs2, f1, Np, 'Digital Filter With High Pass Using Hamming Window(fs = 2fc)');
firlHighPass(15, 12, xn3, fs3, f1, Np, 'Digital Filter With High Pass Using Hamming Window(fs < 2fc)');
%% Experiment 9: Design a digital filter with low pass using hamming window
fpz = 23; fsz = 28;
firlLowPass(fpz, fsz, xn1, fs1, f1, Np, 'Digital Filter With Low Pass Using Hamming Window(fs = 3fc)');
firlLowPass(fpz, fsz, xn2, fs2, f1, Np, 'Digital Filter With Low Pass Using Hamming Window(fs = 2fc)');
firlLowPass(13, 17, xn3, fs3, f1, Np, 'Digital Filter With Low Pass Using Hamming Window(fs < 2fc)');
%% Experiment 10: Design a digital filter with band pass using hamming window
fpl = 15; fpu = 25;
firlBandPass(fpl, fpu, xn1, fs1, f1, Np, 'Digital Filter With Band Pass Using Hamming Window(fs = 3fc)');
firlBandPass(fpl, fpu, xn2, fs2, f1, Np, 'Digital Filter With Band Pass Using Hamming Window(fs = 2fc)');
firlBandPass(7, 15, xn3, fs3, f1, Np, 'Digital Filter With Band Pass Using Hamming Window(fs < 2fc)');
%% Experiment 11: Design a digital filter with band stop using hamming window
fsl = 15; fsu = 25;
firlBandStop(fsl, fsu, xn1, fs1, f1, Np, 'Digital Filter With Band Stop Using Hamming Window(fs = 3fc)');
firlBandStop(fsl, fsu, xn2, fs2, f1, Np, 'Digital Filter With Band Stop Using Hamming Window(fs = 2fc)');
firlBandStop(7, 15, xn3, fs3, f1, Np, 'Digital Filter With Band Band Stop Hamming Window(fs < 2fc)');
function xn = ProduceSamplingSignal(f1, f2, f3, fs, Np, Alltitle)
% Function Description: 
%        We want to make a digital signal composed of three frequency
%        components and sample the produced signal.
% Inputs: 
%        f1, f2, f3: means our selected frequency components, fs
%            represents the sampling frequency.
%        Np: means the number of periods.
% Outputs:
%        xn: represents the sampled signal.

    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    % t = 0: 0.0001: T;     % analog time sequence

    % Step I: Produce digital signal
    xt = cos(2*pi*f1*t) + cos(2*pi*f2*t) + cos(2*pi*f3*t);
    % Step II: Sample produced signal
    xn = cos(2*pi*f1*t) + cos(2*pi*f2*t) + cos(2*pi*f3*t);

    % Step III: Visualize produced signal and sampled signal
    figure('Position', [210, 80, 950, 750]);
    subplot(2, 1, 1);
    plot(t, xt);
    title('Time-domain signal $x(t)$', 'Interpreter', 'latex', 'FontSize', 12);
    xlabel('$t/s$', 'Interpreter', 'latex', 'FontSize', 12);
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    ylim([-2.5, 3.5]);
    grid on

    subplot(2, 1, 2);
    stem(t, xn);
    title('Time-domain sampled signal $x(n)$', 'Interpreter', 'latex', 'FontSize', 12);
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    xlabel('$t/s$', 'Interpreter', 'latex', 'FontSize', 12);
    ylim([-2.5, 3.5]);
    grid on
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function DFTAnalysis(xn, fs, Alltitle)
% Function Description:
%       This function calculates the DFT[x(n)] and do spectral analysis.
% Inputs:
%       xn: digital discrete signal
%       fs: sampling frequency
% Outputs:
%       No return

    N = length(xn);    % number of sampling points
    df = fs / N;       % spectral resolution
    f1 = (0:N-1)*df;    % tranverse to the frequncy sequence
    f2 =  2*(0:N-1)/N;
    % DFT using FFT algorithm
    Xk = fft(xn, N);   
    % Tranverse to the real amplitude
    RM = 2*abs(Xk)/N;
    Angle = angle(Xk);

    figure('Position', [210, 80, 950, 750]);
    % Amplitude-Frequency Characteristics
    subplot(4,1,1);
    stem(f1, RM,'.');
    title('Amplitude-Frequency Characteristics', 'Interpreter', 'latex', 'FontSize', 12);
    xlabel('$f$/Hz', 'Interpreter', 'latex', 'FontSize', 12);
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;
    
    % Phase-Frequency Characteristics
    subplot(4,1,2);
    stem(f1, Angle,'.'); 
    line([(N-1)*df, 0],[0,0]);
    title('Phase-Frequency Characteristics', 'Interpreter', 'latex', 'FontSize', 12);
    xlabel('$f$/Hz', 'Interpreter', 'latex', 'FontSize', 12);
    ylabel('Phase', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    % Amplitude-Frequency Characteristics
    subplot(4,1,3);
    plot(f2, abs(Xk));
    title('Amplitude-Frequency Characteristics', 'Interpreter', 'latex', 'FontSize', 12);
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;
    
    % Phase-Frequency Characteristics
    subplot(4,1,4);
    plot(f2, Angle);
    title('Phase-Frequency Characteristics', 'Interpreter', 'latex', 'FontSize', 12);
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('Phase', 'Interpreter', 'latex', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function ellipBandPass(fpl, fpu, fsl, fsu, rp, rs, x, fs, f1, Np, Alltitle)
    wp = [2*fpl/fs, 2*fpu/fs];
    ws = [2*fsl/fs, 2*fsu/fs];
    [N, wn] = ellipord(wp, ws, rp, rs);    % 获取阶数和截止频率
    [B, A] = ellip(N, rp, rs, wn, 'bandpass');         % 获得转移函数系数

    filter_bp_s = filter(B, A, x);
    X_bp_s = abs(fft(filter_bp_s));
    X_bp_s_angle = angle(fft(filter_bp_s));
    
    % plot the graphs    
    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    N = length(x);    % number of sampling points
    f =  2*(0:N-1)/N;

    % 带通滤波器频谱特性
    figure('Position', [210, 80, 950, 750]);
    subplot(4,4,[1,2,5,6]);
    M = 512;
    wk = 0:pi/M:pi;
    Hz = freqz(B,A,wk);
    plot(wk/pi, 20*log10(abs(Hz)));
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('$20lg|Hg(\omega)|$', 'Interpreter', 'latex', 'FontSize', 12);
    title('带通滤波器频谱特性');
    axis([0.2,0.9,-80,20]);set(gca,'Xtick',0:0.1:1,'Ytick',-80:20:20);
    grid on;

    subplot(4,4,[3,4,7,8]);
    plot(t, filter_bp_s);
    xlabel('t/s', 'Interpreter', 'latex', 'FontSize', 12);
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[9, 10, 11, 12]);
    plot(f, X_bp_s);
    title('带通滤波后频域幅度特性');
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[13, 14, 15, 16]);
    plot(f, X_bp_s_angle);
    title('带通滤波后频域相位特性');    
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('Phase', 'Interpreter', 'latex', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function ellipHighPass(fpz, fsz, rp, rs, x, fs, f1, Np, Alltitle)
    wpz = 2*fpz/fs;
    wsz = 2*fsz/fs;
    [N, wn] = ellipord(wpz, wsz, rp, rs);    % 获取阶数和截止频率
    [B, A] = ellip(N, rp, rs, wn, 'high');         % 获得转移函数系数

    filter_hp_s = filter(B, A, x);
    X_hp_s = abs(fft(filter_hp_s));
    X_hp_s_angle = angle(fft(filter_hp_s));
    
    % plot the graphs    
    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    N = length(x);    % number of sampling points
    f =  2*(0:N-1)/N;

    % 高通滤波器频谱特性
    figure('Position', [210, 80, 950, 750]);
    subplot(4,4,[1,2,5,6]);
    M = 512;
    wk = 0:pi/M:pi;
    Hz = freqz(B,A,wk);
    plot(wk/pi, 20*log10(abs(Hz)));
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('$20lg|Hg(\omega)|$', 'Interpreter', 'latex', 'FontSize', 12);
    title('高通滤波器频谱特性');
    axis([0.2,0.8,-80,20]);
    set(gca,'Xtick',0:0.1:1,'Ytick',-80:20:20);
    grid on;

    subplot(4,4,[3,4,7,8]);
    plot(t, filter_hp_s);
    title('高通滤波后时域图形');
    xlabel('t/s', 'Interpreter', 'latex', 'FontSize', 12);
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[9, 10, 11, 12]);
    plot(f, X_hp_s);
    title('高通滤波后频域幅度特性');
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[13, 14, 15, 16]);
    plot(f, X_hp_s_angle);
    title('高通滤波后频域相位特性');    
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('Phase', 'Interpreter', 'latex', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function ellipLowPass(fpz, fsz, rp, rs, x, fs, f1, Np, Alltitle)
    wpz = 2*fpz/fs;
    wsz = 2*fsz/fs;
    [N, wn] = ellipord(wpz, wsz, rp, rs);    % 获取阶数和截止频率
    [B, A] = ellip(N, rp, rs, wn, 'low');         % 获得转移函数系数

    filter_hp_s = filter(B, A, x);
    X_hp_s = abs(fft(filter_hp_s));
    X_hp_s_angle = angle(fft(filter_hp_s));
    
    % plot the graphs    
    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    N = length(x);    % number of sampling points
    f =  2*(0:N-1)/N;

    % 低通滤波器频谱特性
    figure('Position', [210, 80, 950, 750]);
    subplot(4,4,[1,2,5,6]);
    M = 512;
    wk = 0:pi/M:pi;
    Hz = freqz(B,A,wk);
    plot(wk/pi, 20 * log10(abs(Hz)));
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('$20lg|Hg(\omega)|$', 'Interpreter', 'latex', 'FontSize', 12);
    title('低通滤波器频谱特性');
    axis([0.2,0.9,-80,20]);
    set(gca,'Xtick',0:0.1:1,'Ytick',-80:20:20)
    grid on;

    subplot(4,4,[3,4,7,8]);
    plot(t, filter_hp_s);
    title('低通滤波后时域图形');
    xlabel('t/s', 'Interpreter', 'latex', 'FontSize', 12);
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[9, 10, 11, 12]);
    plot(f, X_hp_s);
    title('低通滤波后频域幅度特性');
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[13, 14, 15, 16]);
    plot(f, X_hp_s_angle);
    title('低通滤波后频域相位特性');   
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('Phase', 'Interpreter', 'latex', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function ellipBandStop(fpl, fpu, fsl, fsu, rp, rs, x, fs, f1, Np, Alltitle)
    wp = [2*fpl/fs, 2*fpu/fs];
    ws = [2*fsl/fs, 2*fsu/fs];
    [N, wn] = ellipord(wp, ws, rp, rs);    % 获取阶数和截止频率
    [B, A] = ellip(N, rp, rs, wn, 'stop');         % 获得转移函数系数

    filter_bp_s = filter(B, A, x);
    X_bp_s = abs(fft(filter_bp_s));
    X_bp_s_angle = angle(fft(filter_bp_s));
    
    % plot the graphs    
    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    N = length(x);    % number of sampling points
    f =  2*(0:N-1)/N;

    % 带阻滤波器频谱特性
    figure('Position', [210, 80, 950, 750]);
    subplot(4,4,[1,2,5,6]);
    M = 512;
    wk = 0:pi/M:pi;
    Hz = freqz(B,A,wk);
    plot(wk/pi, 20*log10(abs(Hz)));
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('$20lg|Hg(\omega)|$', 'Interpreter', 'latex', 'FontSize', 12);
    title('带阻滤波器频谱特性');
    axis([0.2,0.9,-80,20]);
    set(gca,'Xtick',0:0.1:1,'Ytick',-80:20:20);
    grid on;
   
    subplot(4,4,[3,4,7,8]);
    plot(t, filter_bp_s);
    title('带阻滤波后时域图形');
    xlabel('t/s', 'Interpreter', 'latex', 'FontSize', 12);
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[9, 10, 11, 12]);
    plot(f, X_bp_s);
    title('带阻滤波后频域幅度特性');
    ylabel('Amplitude', 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[13, 14, 15, 16]);
    plot(f, X_bp_s_angle);
    title('带阻滤波后频域相位特性');    
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('Phase', 'Interpreter', 'latex', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function firlHighPass(fpz, fsz, x, fs, f1, Np, Alltitle)
    wpz = 2 * pi * fpz / fs;
    wsz = 2 * pi * fsz / fs;
    DB = wpz - wsz;              % 计算过渡带宽度
    N0 = ceil(6.2 * pi / DB);    % 计算所需h(n)长度N0
    N = N0 + mod(N0 + 1, 2);     % 确保h(n)长度N是奇数

    wc = (wpz + wsz) /2 / pi;    % 计算理想高通滤波器通带截止频率
    hn = fir1(N-1, wc, 'high', hamming(N));

    filter_hp_s = filter(hn, 1, x);
    X_hp_s = abs(fft(filter_hp_s));
    X_hp_s_angle = angle(fft(filter_hp_s));
    
    % plot the graphs    
    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    N = length(x);    % number of sampling points
    f =  2*(0:N-1)/N;

    figure('Position', [210, 80, 950, 750]);
    subplot(4,4,[1,2,5,6]);
    M = 1024;
    k = 1:M / 2;
    wk = 2*(0:M/2-1)/M;
    Hz = freqz(hn, 1);
    plot(wk, 20*log10(abs(Hz(k))));
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('$20lg|Hg(\omega)|$', 'Interpreter', 'latex', 'FontSize', 12);
    title('高通滤波器频谱特性')
    axis([0.2,0.8,-80,20]);
    set(gca,'Xtick',0:0.1:1,'Ytick',-80:20:20)
    grid on;
   
    subplot(4,4,[3,4,7,8]);
    plot(t, filter_hp_s);
    title('高通滤波后时域图形');
    txt = xlabel('t/s', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    txt = ylabel('Amplitude', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[9, 10, 11, 12]);
    plot(f, X_hp_s);
    title('高通滤波后频域幅度特性');
    txt = ylabel('Amplitude', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[13, 14, 15, 16]);
    plot(f, X_hp_s_angle);
    title('高通滤波后频域相位特性');    
    txt = ylabel('Phase', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    xlabel('\omega/\pi', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function firlLowPass(fpz, fsz, x, fs, f1, Np, Alltitle)
    wpz = 2 * pi * fpz / fs;
    wsz = 2 * pi * fsz / fs;
    DB = wsz - wpz;           
    N0 = ceil(6.2 * pi / DB);
    N = N0 + mod(N0 + 1, 2);     

    wc = (wpz + wsz) / 2 / pi;
    hn = fir1(N-1, wc, 'low', hamming(N));

    filter_hp_s = filter(hn, 1, x);
    X_hp_s = abs(fft(filter_hp_s));
    X_hp_s_angle = angle(fft(filter_hp_s));
    
    % plot the graphs    
    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    N = length(x);    % number of sampling points
    f =  2*(0:N-1)/N;

    figure('Position', [210, 80, 950, 750]);
    subplot(4,4,[1,2,5,6]);
    M = 1024;
    k = 1:M / 2;
    wk = 2*(0:M/2-1)/M;
    Hz = freqz(hn, 1);
    plot(wk, 20*log10(abs(Hz(k))));
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('$20lg|Hg(\omega)|$', 'Interpreter', 'latex', 'FontSize', 12);
    title('低通滤波器频谱特性')
    axis([0.2,0.9,-80,20]);
    set(gca,'Xtick',0:0.1:1,'Ytick',-80:20:20)
    grid on;
   
    subplot(4,4,[3,4,7,8]);
    plot(t, filter_hp_s);
    title('低通滤波后时域图形');
    txt = xlabel('t/s', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    txt = ylabel('Amplitude', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[9, 10, 11, 12]);
    plot(f, X_hp_s);
    title('低通滤波后频域幅度特性');
    txt = ylabel('Amplitude', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[13, 14, 15, 16]);
    plot(f, X_hp_s_angle);
    title('低通滤波后频域相位特性');    
    txt = ylabel('Phase', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    xlabel('\omega/\pi', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function firlBandPass(fpl, fpu, x, fs, f1, Np, Alltitle)
    wpl = 2 * fpl / fs;
    wpu = 2 * fpu / fs;
    fpass = [wpl, wpu];
    N = 111;
    hn = fir1(N-1, fpass, 'bandpass', hamming(N));

    filter_hp_s = filter(hn, 1, x);
    X_hp_s = abs(fft(filter_hp_s));
    X_hp_s_angle = angle(fft(filter_hp_s));
    
    % plot the graphs    
    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    N = length(x);    % number of sampling points
    f =  2*(0:N-1)/N;

    figure('Position', [210, 80, 950, 750]);
    subplot(4,4,[1,2,5,6]);
    M = 1024;
    k = 1:M / 2;
    wk = 2*(0:M/2-1)/M;
    Hz = freqz(hn, 1);
    plot(wk, 20*log10(abs(Hz(k))));
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('$20lg|Hg(\omega)|$', 'Interpreter', 'latex', 'FontSize', 12);
    title('带通滤波器频谱特性')
    axis([0.2,0.9,-80,20]);
    set(gca,'Xtick',0:0.1:1,'Ytick',-80:20:20)
    grid on;
   
    subplot(4,4,[3,4,7,8]);
    plot(t, filter_hp_s);
    title('带通滤波后时域图形');
    txt = xlabel('t/s', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    txt = ylabel('Amplitude', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[9, 10, 11, 12]);
    plot(f, X_hp_s);
    title('带通滤波后频域幅度特性');
    txt = ylabel('Amplitude', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[13, 14, 15, 16]);
    plot(f, X_hp_s_angle);
    title('带通滤波后频域相位特性');    
    txt = ylabel('Phase', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    xlabel('\omega/\pi', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end
function firlBandStop(fsl, fsu, x, fs, f1, Np, Alltitle)
    wsl = 2 * fsl / fs;
    wsu = 2 * fsu / fs;
    fstop = [wsl, wsu];
    N = 111;
    hn = fir1(N-1, fstop, 'stop', hamming(N));

    filter_hp_s = filter(hn, 1, x);
    X_hp_s = abs(fft(filter_hp_s));
    X_hp_s_angle = angle(fft(filter_hp_s));
    
    % plot the graphs    
    period = 1/f1;        % the period of analog signal(assuming f1 is the minimal)
    T = Np*period;         % sampling time-domain window(several periods)
    Ts = 1 / fs;          % sampling timestep
    t = 0: Ts : T;       % samping sequence of discrete sampling points
    N = length(x);    % number of sampling points
    f =  2*(0:N-1)/N;

    figure('Position', [210, 80, 950, 750]);
    subplot(4,4,[1,2,5,6]);
    M = 1024;
    k = 1:M / 2;
    wk = 2*(0:M/2-1)/M;
    Hz = freqz(hn, 1);
    plot(wk, 20*log10(abs(Hz(k))));
    xlabel('\omega/\pi', 'FontSize', 12);
    ylabel('$20lg|Hg(\omega)|$', 'Interpreter', 'latex', 'FontSize', 12);
    title('带阻滤波器频谱特性')
    axis([0.2,0.9,-80,20]);
    set(gca,'Xtick',0:0.1:1,'Ytick',-80:20:20)
    grid on;
   
    subplot(4,4,[3,4,7,8]);
    plot(t, filter_hp_s);
    title('带阻滤波后时域图形');
    txt = xlabel('t/s', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    txt = ylabel('Amplitude', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[9, 10, 11, 12]);
    plot(f, X_hp_s);
    title('带阻滤波后频域幅度特性');
    txt = ylabel('Amplitude', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;

    subplot(4,4,[13, 14, 15, 16]);
    plot(f, X_hp_s_angle);
    title('带阻滤波后频域相位特性');    
    txt = ylabel('Phase', 'FontSize', 12);
    ylim([-3.5, 3.5]);
    xlabel('\omega/\pi', 'FontSize', 12);
    set(txt, 'Interpreter', 'latex', 'FontSize', 12);
    grid on;
    sgtitle(Alltitle, 'FontName', 'Times New Roman', 'FontSize', 14);
end

Contributors

posted @   LZHMS  阅读(8)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Windy Hill 羽肿
Windy Hill - 羽肿
00:00 / 00:00
An audio error has occurred.

暂无歌词