xcorr command makes mistake at 10^-5 order.
Show older comments
function t = xcoor_time_interval(s1, s2, Fs, tit, plot_wanted)
%XCOOR_TİME_İNTERVAL Summary of this function goes here
% Detailed explanation goes here
% Xcoor is put cross correlation of two signals
[c,lags] = xcorr(s1,s2);
% If it wanted xcoor is plotted
if plot_wanted
figure
plot(lags,c);
title(tit);
grid on
end
[~,iLag] = max(abs(c));
t = -(1/Fs)*(lags(iLag));
end
This was my function and main function is:
clc;
clear all;
close all;
addpath('functions');
% Import audio file
filename = '481583__malatestiniccom__gun-3.wav';
% Get audio signal and signals sampling frequancy in Hz
[s1,Fs] = audioread(filename);
s1 = s1(:,1);
s2 = delayseq(s1, -0.00120612644267, Fs);
s3 = delayseq(s1, 0.00102887528457, Fs);
s4 = delayseq(s1, -0.00023602278035, Fs);
s5 = delayseq(s1, 0.00003558800237, Fs);
% Fs = 44100;
% w = 10;
%
% x1 = 0:1/Fs:1;
% x2 = x1 + 0.00120612644267;
% x3 = x1 - 0.00102887528457;
% x4 = x1 + 0.00023602278035;
% x5 = x1 - 0.00003558800237;
%
% s1 = sin(2*pi*x1);
% s2 = sin(2*pi*x2);
% s3 = sin(2*pi*x3);
% s4 = sin(2*pi*x4);
% s5 = sin(2*pi*x5);
plot_wanted = input("Should I plot the figures[true/false]: "); % Determine function should be plot the figure
% if plot_wanted
% figure
% plot(s1);
% hold on
% plot(s2);
% hold on
% plot(s3);
% hold on
% plot(s4);
% hold on
% plot(s5);
% end
% Get time difference by using xcoor refernce is s1 signal
t12 = xcoor_time_interval(s1, s2, Fs, 's1-s2', plot_wanted);
t13 = xcoor_time_interval(s1, s3, Fs, 's1-s3', plot_wanted);
t14 = xcoor_time_interval(s1, s4, Fs, 's1-s4', plot_wanted);
t15 = xcoor_time_interval(s1, s5, Fs, 's1-s5', plot_wanted);
% t12 = vpa(-0.00120612644267,12);
% t13 = vpa(0.00102887528457,12);
% t14 = vpa(-0.00023602278035,12);
% t15 = vpa(0.00003558800237,12);
% t12 = -0.0012;
% t13 = 0.0010;
% t14 = -2.2676e-04;
% t15 = 4.5351e-05;
and like it is shown here the xcorr command makes mistake at the 10^-5 order and it effects my solution much. How can i solve that?
Accepted Answer
More Answers (0)
Categories
Find more on Correlation and Convolution in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!