Finding the difference between two signals
matlab, signal-processing
Solution
Now there are two functions in Matlab:
one called finddelay
and another called alignsignals that can do what you want, I believe.
Problem
I have two signals, let's call them 'a' and 'b'. They are both nearly identical signals (recorded from the same input and contain the same information) however, because I recorded them at two different 'b' is time shifted by an unknown amount. Obviously, there is random noise in each. Currently, I am using cross correlation to compute the time shift, however, I am still getting improper results. Here is the code I am using to calculate the time shift: ``` function [ diff ] = FindDiff( signal1, signal2 ) %FINDDIFF Finds the difference between two signals of equal frequency %after an appropritate time shift is applied % Calculates the time shift between two signals of equal frequency % using cross correlation, shifts the second signal and subtracts the % shifted signal from the first signal. This difference is returned. length = size(signal1); if (length ~= size(signal2)) error('Vectors must be equal size'); end t = 1:length; tx = (-length+1):length; x = xcorr(signal1,signal2); [mx,ix] = max(x); lag = abs(tx(ix)); shifted_signal2 = timeshift(signal2,lag); diff = signal1 - shifted_signal2; end function [ shifted ] = timeshift( input_signal, shift_amount ) input_size = size(input_signal); shifted = (1:input_size)'; for i = 1:input_size if i <= shift_amount shifted(i) = 0; else shifted(i) = input_signal(i-shift_amount); end end end plot(FindDiff(a,b)); ``` However the result from the function is a period wave, rather than random noise, so the lag must still be off. I would post an image of the plot, but imgur is currently not cooperating. Is there a more accurate way to calculate lag other than cross correlation, or is there a way to improve the results from cross correlation?