matlab - How to directly calculate desired phase shift of signals? -


i'm comparing 2 combined phase shifted signals (x_phase_sig_comb) given signal (x_given). @ moment i'm using for loop add each , every combined phase shifted signal (x_phase_sig_comb) , calculate rmse (root mean square error) value respect given signal (x_given) place each value array , sort least rmse (root mean square error value).

is there algebraic way directly calculate phase shift @ 2 combined phase shifted signals (x_phase_sig_comb) have least, largest, or selected range of rms (root mean square error) when compared of given signal (x_given). i'm trying avoid having calculate each , every combined phase shifted signal (x_phase_sig_comb)

the reason ask have signals large , loop through each 1 take considerable amount of time. directly calculating phase shift give me least, largest or range of rmse values respect (x_given) using type of algebric "form" save considerable amount of time. don't know how constrain frequency , amplitude of phase shifted signals or how algebraically this. ideas?

i created animation below showing 2 signals being phase shifted along rmse values respect (x_given) signal. note: 2 signals on top plot same difference 1 signal being phase shifted left , other signal being phase shifted right.

animated gif

ps: i'm using octave 4.0 similar matlab see example code below

%test compare signals  fs = 100;                    % sampling frequency t = 1/fs;                     % sample time l = 100;                     % length of signal t = (0:l-1)*t;                % time vector t_plot=linspace(0,fs,fs);     %used plotting  x = .5*sin(2*pi*10*t)+0.7*sin(2*pi*12.3*t) +.4*sin(2*pi*16.5*t); %main signal x_given=.4*sin(2*pi*15*t); %given signal compare data_array=[];  rr=1:1:100   x_sig_main_a = circshift(x(:),rr); %shift signal right   x_sig_main_b = circshift(x(:),-rr); %shift signal left   x_phase_sig_comb=x_sig_main_a+x_sig_main_b; %combine shifted signals    nfft = 2^nextpow2(l); % next power of 2 length of siganl   y = fft(x_phase_sig_comb,nfft)/l;   freq = fs/2*linspace(0,1,nfft/2+1);    %find maximum value, should fundamental frequency (approximated)   [maxidx,maxval]=max(2*abs(y(1:nfft/2+1)));   freq(maxval); %max frequency value    %rmse   dy = abs(x_phase_sig_comb(:)-x_given(:)); %absolute error   mae  = mean(dy); %mean-absolute-error   mxe = max(dy); %maximum-absolute-error   rmse = sqrt(mean(dy.^2));  %   root-mean-sqare-error     %data_tmp=[rr,freq(maxval)];   data_tmp=[rr,rmse];   data_array=[data_array;data_tmp];     % plot .   subplot(2,1,1);   plot(t_plot,x_sig_main_a,'-r',t_plot,x_sig_main_b,'-b')   s1 = strcat('phase shift of 2 signals',' - (',num2str(rr),' bits) shifted out of 100' );   titletxt = {s1};    title(titletxt,'fontsize',14);    subplot(2,1,2); plot(data_array(rr,1),data_array(rr,2),'*-r')    axis([0 100 0 1.5])   s1 = strcat('rmse of 2 combined phase shifted signals given signal =',num2str(data_array(rr,2)));   titletxt = {s1};   hold on    title(titletxt,'fontsize',14);   xlabel('phase shift of signals')   ylabel('root mean square erro')    pause(.01) end subplot(2,1,2); plot(data_array(:,1),data_array(:,2),'*-r'); %connect lines in plot  data_array_sort = sortrows(data_array,2); %sort least different 2  data_array_sort(1:3,:) %similar combined phase shifted signals x_given signal 

is there algebraic way directly calculate phase shift @ 2 combined phase shifted signals (x_phase_sig_comb) have least, largest or selected range of rms (root mean square error) when compared of given signal (x_given). i'm trying avoid having calculate each , every combined phase shifted signal (x_phase_sig_comb)

the reason ask have signals large , loop through each 1 take considerable amount of time. directly calculating phase shift give me least, largest, or range of rmse values respect (x_given) using type of algebric "form" save considerable amount of time. don't know how constrain frequency , amplitude of phase shifted signals or how algebraically this. ideas?

maybe can use correlation in stead of rms.

[correlation,lag] = xcorr(x,x_given) [~,index] = max(abs(correlation)) timeshift = lag(index) 

documentation de function xcorr


Comments