How do I extract phase from an FFT?

I am new to both Matlab and fft analyses and I was wondering two things. 1) whether the code is written without errors. It appears to work properly but I wonder if I have included the right steps in the fft processing and 2) how do I extract the phase information? I can not seem to get the phase information using the angle() function, as it results in a very noisy phase signal. Alternatively using an atan() function did not succeed as well.
Thanks!
I have written the following code:
time=linspace(0,8760,8760);
% A is an input signal from an external model
A_noDC=A-mean(A); %This cuts the 0 Hz DC component
f=1/3600; %frequency is 1 sample per hour, thus 1/3600 per second
N=length(A);
X=fft(A_noDC);
X_mag=abs(X);
X_amp=X_mag/N; %normalises magnitude into amplitude
X_ampsingle=X_amp(1:N/2+1); %divides the plot from two-sided to one
X_ampsingle(2:end-1)=2*X_ampsingle(2:end-1); %idem
freq=(0:(N/2))*f/N; %plots the frequency on the x-axis rather than bins
figure('visible','on')
plot(freq,X_ampsingle);
title('Frequency spectrum')
xlabel({'Frequency','[Hertz]'});
ylabel({'Amplitude [MW]'});
figure('visible','on');
plot(time,A); %plots original signal
title('original signal');
end
Update: Thanks for the comments! I will look into that. Nevertheless, the following code appears to show logical results as well, where the threshold is merely used to cut low-amplitude (<X/10) sinusoids from the phase plot. Does this seem right?
X2=X
threshold=max(abs(X)/10);
X2(abs(X)<threshold)=0; % determines the low-amplitude threshold
phase=atan2(imag(X2),real(X2))*180/pi;
phase_single=phase(1:N/2+1); %turns the plot into a one-sided plot
phase_single(2:end-1)=2*phase_single(2:end-1);
figure('visible','on')
plot(freq,phase_single);
title('Phase information')
xlabel({'frequency','[Hertz]'});
ylabel({'phase [degrees]'});

2 Comments

What do you need phase for?
In the context of fluctuating renewable energy generation, I am aiming to isolate (high-amplitude) sinusoids from the frequency spectrum to show the fluctuation of e.g. electricity demand curves and generation profiles.
It this context it matters significantly at what specific hour/day/month a peak of the sinusoid occurs since this could indicate whether demand and supply are in phase or whether they alternate.
I believed this information was described in the phase of the signal. But as said, I am not yet an expert in the field. Any help is welcome!

Sign in to comment.

Asked:

on 25 Oct 2017

Edited:

on 25 Oct 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!