Frequency Domain Change Phase (MatLab)

I posted this question on dsp.stackexchange and was informed that this is more relevant for stackoverflow, since this is primarily a programming issue:

I am trying to write code that allows me to change the phase of a signal in the frequency domain. However, my conclusion is not entirely correct, so something must be wrong. For a simple example, suppose we have the function y = sin (2 * pi * t) and want to implement the -pi / 2 phase shift. My code is as follows:

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))

All graphs look OK, except for the final graph, which looks like this:

Plot of signal with phase change

, . - , , , ! , Y = Y.*exp(-i*2*pi*f*pi/2);, , .

0
2

( ), :

, , :

y = exp(1i*2*pi*t);

, - , :

% Attempt at phase shift
Y = abs(Y).*exp(1i*angle(Y)-1i*pi/4); % -pi/4 shift

, , , , . , :

figure
plot(t,real(u),'k')
hold on
plot(t,real(y),'r')

real(y) , , , , . pi/4 - ( , ):

this is image description here, how are you?

+4

3 .

  • , . , . 64 10. 6,4 , . , , , .
  • . 62 . .
  • pi/2 N/4, N .

. . M . M = 3.

clear all;
close all;

T = 1;  %length of sampling sequence in s
N = 64; %number of samples
M = 3; % number of periods per sequence
ts = T/N; %sample interval
fs = 1/ts %sampling frequency
tmax = (N-1)*ts;
t = 0:ts:tmax;
y = sin(2*pi*M*t);

fig01 = figure;
plot(t,y);
grid on;

%% We do the FT
Y = fft(y);

%% We create a frequency vector in natural order
% -fs/2, ..., 0, ... +fs/2
f =fftshift(( 0:(fs-1)) - fs/2);

%% Show Magnitude spectrum
% There shold be only two lines at -M and +M
figure;
plot(f,abs(Y),'o');
grid on;

%% Attempt at phase shift
% y/t) -> Y(w)
% y(t-t0) -> Y(w) * exp(-i*w*t0)
% Phase shift of pi/2 in frequncy domain is equavalent to as time shift
% of T/4 in time domain

Y = Y.*exp(-i*2*pi*f*T/4);

% Inverse FT
u = ifft(Y);

figure
hold on;
plot(t,real(u),'b-');
plot(t,real(y),'r-');
hold off;
grid;

input signal three periods of a sine signal

spectrum of input signal.  Frequency lines at -3 and +3

. -3 +3

input signal (blue) and phase shifted signal (red)

() ()

+3

Source: https://habr.com/ru/post/1607443/


All Articles