常用序列的MATLAB实现
单位抽样序列
function [x,n] = impseq(n0,nl,nh)
% 产生x(n) = δ(n - n0);nl <= n0 <= nh
% -------------------------------------
% [x,n] = impseq(n0,nl,nh)
%
if((n0 < nl)||(n0 > nh)||(nl > nh))
error('Not:n1 <= n0 <= n2')
end
n = nl:nh;% x = (n - n0) == 0;
x = n == n0;
输入1:
[x, n] = impseq(0,-2,2)
输出1:
x = [0, 0, 1, 0, 0]
n = [-2, -1, 0, 1, 2]
输入2:
[x, n] = impseq(3,-2,2)
输出2:
错误使用 impseq
Not:n1 <= n0 <= n2
单位阶跃序列
function [x,n] = stepseq(n0,nl,nh)
% 产生x(n) = u(n - n0);nl <= n0 <= nh
% -------------------------------------
% [x,n] = stepseq(n0,nl,nh)
%
if((n0 < nl)||(n0 > nh)||(nl > nh))
error('Not:n1 <= n0 <= n2')
end
n = nl:nh;
x = (n-n0) >= 0;
输入1:
[x, n] = stepseq(0,-3,3)
输出1:
x = [0, 0, 0, 1, 1, 1, 1]
n = [-3, -2, -1, 0, 1, 2, 3]
输入2:
[x, n] = impseq(3,-2,2)
输出2:
错误使用 stepseq
Not:n1 <= n0 <= n2
矩形序列
function [x,n] = RN(n0,nl,nh)
% 产生x(n) = u(n) - u(n - n0);nl <= n <= nh
% -------------------------------------
% [x,n] = RN(n0,nl,nh)
%
n = nl:nh;% x = stepseq(0,nl,nh) - stepseq(n0,nl,nh);
x = zeros(1, nh - nl + 1);
for a = 0:n0 - 1
x = x + impseq(a,nl,nh);
end
输入:
[x, n] = RN(2,-1,3)
输出:
x = [0, 1, 1, 0, 0]
n = [-1, 0, 1, 2, 3]
实指数序列
function [x,n] = powseq(a,nl,nh)
% 产生x(n) = a^n;nl <= n <= nh
% -------------------------------------
% [x,n] = powseq(a,nl,nh)
%
n = nl:nh;
x = a.^n;
输入:
[x, n] = powseq(2,-2,2)
输出:
x = [0.25, 0.50, 1.00, 2.00, 4.00]
n = [-2, -1, 0, 1, 2]
复指数序列
clc;clear;
a = 0.4;w = 0.6;
nl = -1;nh = 10;
n = nl:nh;x = exp((a + w*1i)*n);
figure(1)
subplot(2,1,1);stem(n,real(x),'.');
axis([nl - 3,nh + 3,1.2*min(real(x)),1.2*max(real(x))]);grid on;subplot(2,1,2);stem(n,imag(x),'.');
axis([nl - 3,nh + 3,1.2*min(imag(x)),1.2*max(imag(x))]);grid on;
输出:
正余弦序列
clc;clear;
n = 0:12;
x = 4*sin(0.3*pi*n + pi/4) + 7*cos(0.7*pi*n + pi/5);stem(n,x),grid on;
输出:
随机序列
clc;clear;
N = 50;n = 0:N-1;
x = rand(1,N);
y = randn(1,N);figure(1)
subplot(2,1,1);stem(n,x,'.');axis([0,N,-0.2,1.2]);grid on;
subplot(2,1,2);stem(n,y,'.');axis([0,N,1.2*min(y),1.2*max(y)]);grid on;
输出:
周期序列
简单复制
function [xtide, ntide] = expandseq(x,n,k)
xtide = x;
ntide = n;
N = length(x);
for i=1:k-1
xtide = [xtide, x];
ntide = [ntide, n + i*N];
end
输入:
[xtide, ntide] = expandseq([1,2,3],[0,1,2],3)
输出:
xtide = [1, 2, 3, 1, 2, 3, 1, 2, 3]
ntide = [0, 1, 2, 3, 4, 5, 6, 7, 8]
用求余数的方法(模运算)
clc;clear;
x = [1,2,3,4];N = length(x);k = 5;
nx = 0:N-1;ny = 0:k*N-1;
y = x(mod(ny,N) + 1);figure(1)
subplot(211);stem(nx,x,'.');
axis([-1,N,0,5]);grid on;
subplot(212);stem(ny,y,'.');
axis([-1,k*N,0,5]);grid on;
输出:
说明
文章中出现的代码参考:数字信号处理教程/程佩青. 第四版: 清华大学出版社。本人在其代码上有一定改进。本人写这一系列文章纯粹出于自己及广大学子学习MATLAB与数字信号处理的目的。欢迎指正!