一、周期序列的MATLAB编程
1.产生包含x(n)的P行矩阵
2.利用结构( : )将P行连成一个长的行向量。
注:( : )仅对列起作用,所以必须要用矩阵转置符 '
程序
xtilde = x' * ones(1, P) %P代表x周期,x为一个一周期序列
xtilde = xtilde( : ); %长纵列
xtilde = xtilde'; %长行列
二、序列运算
1.信号相加
在MATLAB中用“+”实现。但样本序列x1(n)和x2(n)长度必须相同。
注:如果序列长度不等,或长度相等但样本位置不同,也不能直接用“+”。
function [y, n] = sigadd(x1, n1, x2, n2)
% implements y(n) = x1(n) + x2(n)
%-----------------------------------------
%[y, n] = sigadd(x1, n1, x2, n2)
% y = sum sequence over n, which include n1
% and n2
% x1 = first sequence over n1
% x2 = second sequence over n2(n2 can be
% different from n1)
%
n = min(min(n1), min(n2)) : max(max(n1), max(n2)); %duration of y(n)
y1 = zeros(1, length(n)); y2 = y1; %initialization
y1(find((n >= min(n1)) & (n <= max(n1)) == 1)) = x1; % x1 with duration of y
y2(find((n >= min(n2)) & (n <= max(n2)) == 1)) = x2; % x2 with duration of y
y = y1 + y2;
注:摘自《数字信号处理(MATLAB版)》(第三版)
2.信号相乘
样本序列的相乘(或称点乘),用符号“ .* ”实现,限制条件同上。
function [y, n] = sigmult(x1, n1, x2, n2)
% implements y(n) = x1(n) * x2(n)
% -----------------------------------------------
% [y, n] = sigmult(x1, n1, x2, n2)
% y = product sequence over n, which includes
% n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2(n2 can be
% different from n1)
%
n = min(min(n1), min(n2)) : max(max(n1), max(n2)); % duration of y(n)
y1 = zeros(1, length(n)); y2 = y1; %
y1(find((n >= min(n1)) & (n <= max(n1)) == 1)) = x1; % x1 with duration of y(n)
y2(find((n >= min(n2)) & (n <= max(n2)) == 1)) = x2; % x2 with duration of y(n)
y = y1 .* y2; % sequence multiplication
注:摘自《数字信号处理(MATLAB版)》(第三版)
3.加权
样本序列乘以标量a(a为常数或其他)
用符号“*”实现。
4.移位
实现方法
function [y, n] = sigshift(x, m, k)
% implements y(n) = x(n - k)
% -----------------------------------------------
% [y, n] = sigshift(x, m, k)
%
n = m + k; y = x;
注:摘自《数字信号处理(MATLAB版)》(第三版)
5.反转
function [y, n] = sigfold(x, n)
% implements y(n) = x(-n)
% -----------------------------------------------
% [y, n] = sigfold(x, n)
%
y = fliplr(x); n = - fliplr(n);
注:摘自《数字信号处理(MATLAB版)》(第三版)
6.样本累加
将样本序列x中的n1 : n2之间的全部样本值相加。(非样本序列相加)
由sum(x(n1 : n2))函数实现。
7.样本乘积
将n1 : n2之间的全部样本值连乘起来。
由prod((n1 : n2))函数实现。
8.信号能量
有限长度序列x(n)的能量可由以下两种方法计算
Ex = sum(x .* conj(x)); %方法一
Ex = sum(abs(x) .^ 2); %方法二
注:摘自《数字信号处理(MATLAB版)》(第三版)