title: How to directly calculate desired phase shift of signals?
body: <p>I'm comparing 2 combined phase shifted signals (<em>x_phase_sig_comb</em>) to a given signal (<em>x_given</em>).
  At the moment I'm using a <code>for</code> loop to add together each and every combined phase shifted signal (<em>x_phase_sig_comb</em>) and then I calculate the RMSE (root mean square error) value with respect to the given signal (<em>x_given</em>) I then place each value into an array and then I sort for the least RMSE (Root mean square error value).  </p>
  
  <p><strong>Is there an algebraic way to directly calculate the phase shift at which the 2 combined phase shifted signals (<em>x_phase_sig_comb</em>) will have the least, largest, or a selected range of RMS (root mean square error) when compared to that of a given signal (<em>x_given</em>).  I'm trying to avoid having to calculate each and every combined phase shifted signal (<em>x_phase_sig_comb</em>)</strong> </p>
  
  <p>The reason I ask is that I have signals that are very large and to loop through each one would take a considerable amount of time.  Directly calculating the phase shift that would give me the least, largest or a range of RMSE values with respect to (<em>x_given</em>) using some type of algebric "form" would save a considerable amount of time.  I just don't know how to constrain the frequency and amplitude of the phase shifted signals or how to algebraically do this. any ideas?</p>
  
  <p>I created an animation below showing the two signals being phase shifted along with the RMSE values with respect to the (<em>x_given</em>) signal. <strong>Note: the two signals on the top plot are the same the only difference is one signal is being phase shifted to the left and the other signal is being phase shifted to the right.</strong> </p>
  
  <p><a href="https://i.stack.imgur.com/78mYv.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/78mYv.gif" alt="animated gif"></a> </p>
  
  <p>Ps: I'm using octave 4.0 which is similar to matlab
  See example code below</p>
  
  <pre><code>%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 just for 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 to compare it to
  data_array=[];
  
  for rr=1:1:100
    x_sig_main_a = circshift(x(:),rr); %shift signal to the right
    x_sig_main_b = circshift(x(:),-rr); %shift signal to the left
    x_phase_sig_comb=x_sig_main_a+x_sig_main_b; %combine shifted signals together
  
    NFFT = 2^nextpow2(L); % Next power of 2 from length of siganl
    Y = fft(x_phase_sig_comb,NFFT)/L;
    freq = Fs/2*linspace(0,1,NFFT/2+1);
  
    %find maximum value, it should be the 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 to the 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 by least different 2 
  data_array_sort(1:3,:) %Similar combined phase shifted signals to x_given signal
  </code></pre>
  
  <p><strong>Is there an algebraic way to directly calculate the phase shift at which the 2 combined phase shifted signals (<em>x_phase_sig_comb</em>) will have the least, largest or a selected range of RMS (root mean square error) when compared to that of a given signal (<em>x_given</em>).  I'm trying to avoid having to calculate each and every combined phase shifted signal (<em>x_phase_sig_comb</em>)</strong> </p>
  
  <p>The reason I ask is that I have signals that are very large and to loop through each one would take a considerable amount of time.  Directly calculating the phase shift that would give me the least, largest, or a range of RMSE values with respect to (<em>x_given</em>) using some type of algebric "form" would save a considerable amount of time.  I just don't know how to constrain the frequency and amplitude of the phase shifted signals or how to algebraically do this. any ideas?</p>