• 数字信号处理(MATLAB入门例子)


    (代码主要来源于这本书)

    1.用MATLAB产生32个正弦波样本,A=2,f=1000Hz,以及fs=8000Hz

    1. %
    2. % Example 2.1 Sinewave generator
    3. % This example generate 32 sine sample,
    4. % plot it and save in sine.dat file
    5. % For the book "Real Time Digital Signal Processing:
    6. % Fundamentals, Implementation and Application, 3rd Ed"
    7. % By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
    8. % Publisher: John Wiley and Sons, Ltd
    9. n = [0:31]; % Time index n
    10. omega = 0.25*pi; % Digital frequency
    11. xn = 2*sin(omega*n); % Sinewave generation
    12. plot(n, xn, '-o'); % Samples are marked by 'o'
    13. xlabel('Time index, n');
    14. ylabel('Amplitude');
    15. axis([0 31 -2 2]); % Define ranges of plot
    16. save sine.dat xn -ascii;% Save in ASCII data file

    例2:绘制传递函数的零极点图:

    1. % Example 2.11a Mgnitude and phase response of an IIR filter
    2. % This example plots magnitude and phase response of an IIR filter
    3. % For the book "Real Time Digital Signal Processing:
    4. % Fundamentals, Implementation and Application, 3rd Ed"
    5. % By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
    6. % Publisher: John Wiley and Sons, Ltd
    7. b=[1]; % Define numerator
    8. a=[1, -1, 0.9]; % Denominator
    9. zplane(b,a); % Pole-zero plot

    例3:正弦波A=1,f=1000Hz,采样率为 10 000Hz,可以产生100个正弦波采样样本。信号的幅度谱可以采用以下MATLAB程序绘制:

    1. % example2_16.m - Compute and plot amplitude spectrum of sinewave
    2. %
    3. % For the book "Real Time Digital Signal Processing:
    4. % Fundamentals, Implementation and Application, 3rd Ed"
    5. % By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
    6. % Publisher: John Wiley and Sons, Ltd
    7. N=100; f = 1000; fs = 10000; % Define parameter values
    8. n=[0:N-1]; k=[0:N-1]; % Define time and frequency indices
    9. omega=2*pi*f/fs; % Frequency of sinewave
    10. xn=sin(omega*n); % Generate sinewave
    11. Xk=fft(xn,N); % Perform DFT
    12. magXk=20*log10(abs(Xk)); % Compute magnitude spectrum
    13. plot(k, magXk); axis([0, N/2, -inf, inf]); % plot from 0 to pi
    14. xlabel('Frequency index, k');
    15. ylabel('Magnitude (dB)');

    例4:绘制下面这个传递函数的幅度响应和相位响应

    1. % For the book "Real Time Digital Signal Processing:
    2. % Fundamentals,Implementation and Application, 3rd Ed"
    3. % By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
    4. % Publisher: John Wiley and Sons, Ltd
    5. b=[1, 0, 0, 0, 0, 0, 0, 0, -1]; % Numerator
    6. a=[1, -1]; % Denominator
    7. freqz(b,a); % Plot frequency response

    例5:本例产生中的一个包含噪声的正弦波信号,其中噪声是零均值、单位方差白噪声所示。

    1. N=256; A=sqrt(2); w0=0.2*pi; % Define parameters
    2. n = [0:N-1]; % Time index
    3. sn = A*sin(w0*n); % Sine sequence
    4. sd = 12357; % Define seed value
    5. rng(sd); % Use defined seed
    6. vn = (rand(1,N)-0.5)*sqrt(12); % Zero-mean, unit-variance white noise
    7. xn = sn+vn; % Sinewave embedded in white noise
    8. plot(n,xn);
    9. save xn.dat xn -ascii ;

  • 相关阅读:
    RK3568平台开发系列讲解(调试篇)系统运行相关频率设置
    网络第一颗
    云原生之深入解析如何合并多个kubeconfig文件
    如何恢复电脑硬盘删除数据?提供一套实用恢复方案
    golang知识点整理
    [dp]Matryoshka Doll 2022杭电多校第9场 1007
    LeetCode:5. 最长回文子串
    Redis 多线程操作同一个Key如何保证一致性?
    VsCode高速下载
    koa2 快速上手
  • 原文地址:https://blog.csdn.net/z754721/article/details/138199062