xcorr函数原理_Matlab_xcorr_互相关函数的讨论 [未完成]

本文探讨了如何使用Matlab的xcorr函数计算两个平稳信号的互相关,并通过实例展示了自编函数与内置函数的比较,以及与conv函数的区别。
摘要由CSDN通过智能技术生成

假设两个平稳信号 $\bold{x}$ 和 $\bold{y}$ ,如果 $x\left(t+\tao\right)= y\left(t\right)$ ,则可通过互相关求 $\tao$ 。

首先,通过实现 xcorr 函数介绍互相关计算流程:

clc

clear

close

% 实现 xcorr 函数

% 基本设置

T = 1; % [s] 总时间长度

fs = 5000; % [Hz] 采样频率

t = 0:1/fs:T; % [s] 时间坐标

N = length(t); % 信号个数

% 信号生成

tm = [ t(1:N) - T , t(2:N) ]; % 相关结果的时间延迟坐标轴

td1 = 0.2*T; % x 信号时间延迟

td2 = 0.3*T; % y 信号时间延迟

noise = rand(1,2*N); % 生成了两倍时间 T 长度的噪声 [0,1]噪声

x = noise(1+round(td1*fs):N+round(td1*fs))-0.5*ones(1,N);

y = noise(1+round(td2*fs):N+round(td2*fs))-0.5*ones(1,N);

% 求取互相关

z1 = xcorr(x,y); % Matlab 自带函数

[~,I1] = max(abs(z1));

z2 = zeros(1,N); % 自编函数

for n = 1:length(tm)

z2(n) = sum( x( max(1,n-N+1):min(n,N) ).*y( max(1,N-n+1):min(2*N-n,N) ) );

end

[~,I2] = max(abs(z2));

%--------------------计算说明--------------------%

% case1: | case2: %

% .N | .2*N-n %

% y: .......... | y: .......... %

% .N-n+1 | .1 %

% .n | .N %

% x: .......... | x: .......... %

% .1 | .n-N+1 %

%------------------------------------------------%

err = z1-z2; % 两种算法的差

% 绘图

subplot(1,3,1)

plot(tm,z1)

title(‘Matlab function‘)

xlabel(‘time delay‘)

ylabel(‘Amp‘)

a1 = gca;

a1.XTick = sort([-1:0.5:1 tm(I1)]);

subplot(1,3,2)

plot(tm,z2)

title(‘My function‘)

xlabel(‘time delay‘)

ylabel(‘Amp‘)

a2 = gca;

a2.XTick = sort([-1:0.5:1 tm(I2)]);

subplot(1,3,3)

plot(tm,err,‘.-‘)

title(‘error‘)

xlabel(‘time delay‘)

ylabel(‘Amp‘)

suptitle(‘xcorr realization‘)

20180709200749406633.jpg

clc

clear

close

% 比较 conv xcorr

% 例子

A = ones(1,12);

B = 0:4;

C = xcorr(A,B);

D = conv(A,B);

%绘图

subplot(2,2,1)

plot(A,‘.-‘)

ylim([ -0.1 5.1 ])

xlim([ 0.9 12.1])

title(‘A = ones(1,12)‘)

xlabel(‘n‘)

ylabel(‘Amp‘)

subplot(2,2,2)

plot(B,‘.-‘)

ylim([ -0.1 5.1 ])

xlim([ 0.9 12.1])

title(‘B = 0:4‘)

xlabel(‘n‘)

ylabel(‘Amp‘)

subplot(2,2,3)

plot(C,‘.-‘)

ylim([ -0.1 15.1 ])

xlim([ 0.9 25.1])

title(‘xcorr 结果‘)

xlabel(‘n‘)

ylabel(‘Amp‘)

subplot(2,2,4)

plot(D,‘.-‘)

ylim([ -0.1 15.1 ])

xlim([ 0.9 25.1])

title(‘cone 结果‘)

xlabel(‘n‘)

ylabel(‘Amp‘)

suptitle(‘conv与xcorr对比‘)

20180709200749643947.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值