How can I find phase angle for chosen frequency?

Hi everyone.
I am new in matlab so maybe my question is stupid. I have a two signals rec(t) and sent(t) for which I want to find time delay through phase vs. frequency realtionship obtained from cross spectrum. I obtained cross spectrum through the FFT of the cross corelation between rec(t) and sent(t). Here it is:
time=data(15:length(data),1); %time of measurement - s
sent=data(15:length(data),2); %sent signal - mV
rec=data(15:length(data),3); %recorded signal - mV
samples=length(time); %number of samples
Fs=samples/max(time); %sampling frequency - Hz
dt=max(time)/samples; %time interval - s
freq=(0:samples/2)/samples/dt; %frequency scale for FFT
FFTrec=fft(rec); %FFT of recorded signal
FFTsent=fft(sent); %FFT of sent signal
CorrRecSent=(ifft(FFTrec.*conj(FFTsent))); %cross correlation definition
CS=fft(CorrRecSent); %cross spectrum (CS)
amp=abs(CS); %amplitude of CS
amp1=amp(1:samples/2+1); %amplitude of CS for half of the frequency spectrum
A2=angle(CS);
A1=A2(1:samples/2+1); %phase angle of (CS)
A=unwrap(A1); %unwrapped phase
plot(freq,(A));
xlabel('frequency (Hz)')
ylabel('phase (rad)')
And here is the plot. Is there any command or procedure how can I obtain exact phase angles for given frequencies (marked with black line)? Or how can I find the slope of the drawn orange line? I chose this range of frequencies because my sent signal was 5 kHz, so something around was chosen.
Thanks for help.

 Accepted Answer

I don’t have your data, but I would do something like this:
idxrng = find( (freq >= 2000) & (freq <= 10000));
b = polyfit(freq(idxrng), A(idxrng), 1);
A_4_8 = polyval(b, [4000 8000]);
That should return the estimated phase angles for 4000 and 8000 Hz in ‘A_4_8’.
Note: This is UNTESTED CODE but it should work.

4 Comments

HI Star Rider:) it works thanks a lot may but I have two questions?
1. what means this? idxrng = find( (freq >= 2000) & (freq <= 10000));
2. why you used freq(idxrng) in second row? is it because freq corresponds to x axis?
My pleasure.
1. The idxrng variable selects a vector of the indices of all the frequencies that are between those limits. This selects the data to be passed to polyfit.
2. Correct. In your plot, ‘freq’ is the x-axis (independent variable), and phase angle (that I call ‘A’ here) is the y-axis (dependent variable). See the documentation for polyfit for the details on its function.
yes a i checked it...big thanks to you!!! you learn me new things in matlab:)
As always, my pleasure.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!