I have a problem when I try to use Matlab built-in function for cross-correlation 'xcorr'.
Background: I created two sinusoids, one of them delayed in time. I choose center frequencies of these signals slightly different than another. In this way, I am trying to analyze the effects of frequency mismatch on cross-correlation results. Problem: The problem is when I try to calculate delay using xcorr function in MatLab, I could not find the exact lag. The result is quite far away from my offset-time. I try to increase my signal length or sampling frequency. But nothing changes.
Do you have any idea? Here is my simple code, below.
clc clear all close all Fs=48e3; dt=1/Fs; t=0:dt:(4*Fs-1)*dt; CarrierFreq=5e3; Dev=50; CarrierFreqDev=CarrierFreq +Dev*(2*randi(2,1)-3); Offset=10000; OffsetTime=Offset*dt; NoisePower=-95 DesiredSNR=20; % Signal Power ReceivedSignalPower=NoisePower+DesiredSNR; ReceivedSignalPowerWatt=10^(ReceivedSignalPower/10); NoisePowerWatt=10^(NoisePower/10); % Generate Noise @ specified power Noise1 = wgn(1,length(t),NoisePowerWatt,'linear'); Noise2 = wgn(1, length(t),NoisePowerWatt,'linear'); % Generate Signal1 @ specified power Signal1=cos(2*pi*CarrierFreq*t); pSignal1 = sum(Signal1.^2) / length(Signal1); Signal1 = Signal1/sqrt(pSignal1); Signal1_Norm=sqrt(ReceivedSignalPowerWatt)*Signal1; Signal1=Signal1_Norm+Noise1; % Generate Delayed Signal @ specified power Signal2=cos(2*pi*CarrierFreqDev*t); pSignal2=sum(Signal2.^2) / length(Signal2); Signal2_Norm=sqrt(ReceivedSignalPowerWatt)*Signal2; Signal_Delay=circshift(Signal2_Norm, -Offset); Signal2=Signal_Delay+Noise2; [Coeff, Delay_Time] = xcorr(Signal1,Signal2); %% with dev. [ ~,Indx] = max(abs(Coeff)); Delay_Time = Delay_Time(Indx)*dt;