(代码主要来源于这本书)
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, Ltdn = [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, Ltdb=[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, LtdN=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, Ltdb=[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 ;