% This file shows how signals buried in noise can be detected using % correlation (assuming the noise is random and uncorrelated). % % Here we use two noisy signals (20 Hz sinusoid with noise), % sampled at t_s = 0.01 seconds for 2 seconds. % % Taken from Digital Signal Processing: A Modern Approach by Ashok Ambardar, % Thompson Publishing, ISBN 0-534-40509-6, Copyright 2007 % ts = 0.01;t = 0:ts:2;N1 = length(t); % Set up time array x = sin(2*20*pi*t); % Signal x z1 = 2*randn(size(x));z2 = 2*randn(size(x)); % Random noise x1 = x + z1;x2 = x + z2; % Noisy signals cc = conv(x1,fliplr(x2)); % cross-correlation of x1 and x2 cn = conv(z1,fliplr(z2)); % cross-correlation of noise N = length(cn); % Length of corr tc = -(N1-1):N1-1;f=(0:N-1)/N/ts; % corr and FFT axis values ccspec = abs(fft(cc))/N; % FFT of cross-correlation cnspec = abs(fft(cn))/N; % FFT of noise subplot(2,2,1),plot(tc,cc);ax=axis; % Cross-correlation cc subplot(2,2,2),plot(tc,cn);axis(ax); % Noise correlation cn subplot(2,2,3),plot(f,ccspec);ay=axis; % spectrum of cc subplot(2,2,4),plot(f,cnspec);axis(ay); % spectrum of cn