MATLAB与数字信号处理——常用序列的MATLAB实现


常用序列的MATLAB实现

单位抽样序列

单位阶跃序列

矩形序列

实指数序列

复指数序列

正余弦序列

随机序列

周期序列

简单复制

用求余数的方法(模运算)

说明


常用序列的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与数字信号处理的目的。欢迎指正!

  • 8
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Guo_Zhanyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值