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

本文档展示了如何使用MATLAB实现数字信号处理的基本操作,包括生成正弦波、绘制信号的幅度谱和相位响应、IIR滤波器的零极点图,以及处理包含噪声的信号。这些例子引用自《实时数字信号处理》一书。
摘要由CSDN通过智能技术生成

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

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

%
% Example 2.1 Sinewave generator
% This example generate 32 sine sample,
% plot it and save in sine.dat file

% For the book "Real Time Digital Signal Processing: 
%               Fundamentals, Implementation and Application, 3rd Ed"
%               By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
%               Publisher: John Wiley and Sons, Ltd

n = [0:31];             % Time index n
omega = 0.25*pi;        % Digital frequency
xn = 2*sin(omega*n);    % Sinewave generation
plot(n, xn, '-o');      % Samples are marked by 'o'
xlabel('Time index, n'); 
ylabel('Amplitude');
axis([0 31 -2 2]);		% Define ranges of plot
save sine.dat xn -ascii;% Save in ASCII data file

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

% Example 2.11a Mgnitude and phase response of an IIR filter
% This example plots magnitude and phase response of an IIR filter

% For the book "Real Time Digital Signal Processing: 
%               Fundamentals, Implementation and Application, 3rd Ed"
%               By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
%               Publisher: John Wiley and Sons, Ltd

b=[1];          % Define numerator
a=[1, -1, 0.9]; % Denominator 
zplane(b,a);    % Pole-zero plot

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

% example2_16.m - Compute and plot amplitude spectrum of sinewave
%

% For the book "Real Time Digital Signal Processing: 
%               Fundamentals, Implementation and Application, 3rd Ed"
%               By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
%               Publisher: John Wiley and Sons, Ltd

N=100; f = 1000; fs = 10000; % Define parameter values
n=[0:N-1]; k=[0:N-1];        % Define time and frequency indices
omega=2*pi*f/fs;             % Frequency of sinewave
xn=sin(omega*n);             % Generate sinewave
Xk=fft(xn,N);                % Perform DFT
magXk=20*log10(abs(Xk));     % Compute magnitude spectrum
plot(k, magXk); axis([0, N/2, -inf, inf]); % plot from 0 to pi
xlabel('Frequency index, k');
ylabel('Magnitude (dB)');

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

% For the book "Real Time Digital Signal Processing: 
%               Fundamentals,Implementation and Application, 3rd Ed"
%               By Sen M. Kuo, Bob H. Lee, and Wenshun Tian
%               Publisher: John Wiley and Sons, Ltd

b=[1, 0, 0, 0, 0, 0, 0, 0, -1]; % Numerator
a=[1, -1];                      % Denominator
freqz(b,a);                     % Plot frequency response

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


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

  • 14
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值