title: Change phase of a signal in frequency domain (MatLab)
body: <p>I posted this question on dsp.stackexchange, and was informed that it was more relevant for stackoverflow as it is primarily a programming question:</p>
  
  <p>I am attempting to write a code which allows me to change the phase of a signal in the frequency domain.  However, my output isn't exactly correct, so something must be wrong. For a simple example assume that we have the function y = sin(2*pi*t) and want to implement a phase shift of -pi/2. My code looks as follows:</p>
  
  <pre><code>clear all
  close all
  
  N = 64; %number of samples
  fs = 10; %sampling frequency
  ts = 1/fs; %sample interval
  tmax = (N-1)*ts;
  t = 0:ts:tmax;
  y = sin(2*pi*t);
  
  figure
  plot(t,y)
  
  % We do the FT
  f = -fs/2:fs/(N-1):fs/2;
  Y = fftshift(fft(y));
  
  % Magnitude spectrum
  figure
  plot(f,abs(Y));
  
  phase = angle(Y);
  
  % Phase spectrum
  figure
  plot(f,phase)
  
  Y = ifftshift(Y)
  
  % Attempt at phase shift
  Y = Y.*exp(-i*2*pi*f*pi/2);
  
  % Inverse FT
  u = ifft(Y);
  
  figure
  plot(t,real(u))
  </code></pre>
  
  <p>All plots look OK except for the final plot which looks as follows:</p>
  
  <p><img src="https://i.stack.imgur.com/sjxfD.jpg" alt="Plot of signal with phase change"></p>
  
  <p>This looks almost correct but not quite.  If anyone can give me some pointers as to how my code can be corrected in order to fix this, I would greatly appreciate it!  I have a feeling my mistake has something to do with the line <code>Y = Y.*exp(-i*2*pi*f*pi/2);</code>, but I'm not sure how to fix it.</p>