cross correlation mismatch

3 views (last 30 days)
zozo
zozo on 12 Mar 2012
I am facing a strange situation. I have a signal x (sum of sines and cosines) of length 50000 samples. One hand, I delay it directly by converting it into frequency domain and multiplying it by the delaying exponential,then ifft to produce y. One other hand, I delay it block by block using overlap add method (also in frequency domain) to produce z. Both produce exactly identical results.
%%OLA %%
clc
clear all
close all
fs=50000; % sampling frequency in Hz
f1=500; % frequency of signal in Hz
f2=400;
f3=300;
t_duration=1; %duration of LONG signal in seconds
t = 0:1/fs:t_duration-1/fs; %sampling points
% %x=square(2*pi*f3*(t)); %LONG input signal
x = 10*(sin(2*pi*f1*t)+sin(9*pi*f3*t)+cos(10*pi*300*(t))); %LONG input signal)+s
% % plot(x)
% load('rolo');
% x=mic_in(2,:);
%x=randn(size(t));
d=-0.00656438263; % time delay in seconds
P=1000; %block length
fax = fs*[(0:P/2) (-P/2+1:-1)]/P; % Create frequency shift vector in the frequency domain
h = ifft(exp(-1i*d*2*pi*fax)); %impulse response
L = length(h);
M = length(x);
% Pad x with zeros, as needed, to make M = QL for some integer Q
M_save = M;
r = P-mod(M,L);%%%%%check point%%%%%
M = M + r;
x_z = [x(:)' zeros(1,r)];
Q = M/L;
y = zeros(1,L+M_save-1);
% Compute h_z and H_z
N = 2^nextpow2(2*L); % FFT block length for max. computational efficiency
H_z = fft(h,N); %N-point fft of impulse response 'h'
% Compute y
y0 = zeros(1,L*(Q-1)+N);
y1 = zeros(1,N);
for i = 0 : Q-1
m = i*L;
x_i = x_z(m+1:m+L);
x_iz = [x_i(:)' zeros(1,N-L)];
X_iz = fft(x_iz);
y1 = ifft(X_iz.*H_z);
y0(m+1:m+N) = y0(m+1:m+N) + y1;
end
y = real(y0(1:L+M_save-1));
yd= [y(1:L-1)+y(end-(L-2):end) y(L:end-(L-1))];
tf=(0:length(y)-1)/fs;
y_z = 10*(sin(2*pi*f1*(t-d))+sin(9*pi*f3*(t-d))+cos(10*pi*300*((t-d)))); %analytic
stem(yd-y_z) % verification
But when I perform cross correlation between them using xcorr(x,y) I get peak at the correct number of samples(say 49 for example) but by doing xcorr(x,z), I get the peak at 49-1000=-951 samples. where 1000 is my block length in OLA method.
What is the reason behind this? How can I rectify it?
  4 Comments
Honglei Chen
Honglei Chen on 12 Mar 2012
Then why do you say they produce exactly identical results? Can you explain how you did overlap-add?
zozo
zozo on 12 Mar 2012
I have updated my code in question above. Please have a look. What am I missing?

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!