From: Felix on
I am trying to correlate a loaded audio which contains different tones spaced out at 7 seconds with a kernal created with the users inputs. If I change the length of the kernal, the error in the time delay between the signals changes. How can I eliminate this? Am I finding the delay correctly?


% Asks user for wavfile name, then loads wavfile. Prompts
% user to enter number of tones desired with corresponding
% frequencies from 300 - 3300 Hz range. Generates "kernel"
% for each tone and localizes it. Plots data of localized tone

clear
clc

% Prompts user for the signal file name
file_name=input('Enter file name: ','s');

% Assigns signal to a variable
[Y, fs]=wavread(file_name);
Y=Y';
L=length(Y);
duration= L/fs;

% Plots signal in time domain
t=linspace(0,duration,L);
subplot(2,1,1),plot(t,Y);
title('Audio Signal in Time Domain')
xlabel('Time (Seconds)')
ylabel('Y(t)')

% Sound Signal
%sound(Y,fs);

% Plots single sided spectrum of audio signal
YY=fft(Y)/L;
f=linspace(0,fs,L);
subplot(2,1,2),plot(f(1:(L/2)+1),2*abs(YY(1:(L/2)+1)));
title('Single-Sided Spectrum of Audio Signal')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

% Prompt user for number of tones and corresponding freq
num_tones=input('Enter the number of tones:');
time2=linspace(0,4,4*fs); %THIS WHERE I CHANGE THE LENGTH OF KERNEL

for k=1:num_tones
freq_of_tone=input('Enter frequency between 300 Hz and 3300 Hz:');
ftone=cos(2*pi*freq_of_tone*time2);
sound(ftone,fs);

m2 = Y;
m1 = ftone;
zmax = find(xcorr(m1,m2)==max(xcorr(m1,m2)));
delay = length(m2) - zmax;
delay_t=delay/fs;
abcd=min(delay_t);
e=find(delay_t==abcd);
delay_t=delay_t(e);
fprintf('The time delay is about %f seconds ',delay_t);

% Plots signal in time domain
t=linspace(0,duration,L);
subplot(2,1,1),plot(t,Y);
title('Audio Signal in Time Domain')
xlabel('Time (Seconds)')
ylabel('Y(t)')

%Plots and plays only the frequency input in time domain
Y_new=m2;
Y_new(1:delay)=0;
Y_new(delay+length(ftone):end)=0;
subplot(2,1,2),plot(t,Y_new);
title('New Signal')
xlabel('Time (Seconds)')
ylabel('Y(t)')
sound(Y_new,fs);
end