You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Detecting phase difference between two channels
3 views (last 30 days)
Show older comments
Hi
I dont have a lot of experiance using MATLAB, Can someone please show me how to detect the phase differance between my 2 signals attached, number of samples are 6000 and sample rate is 0.01
Any Help would be greatly appreciated.
Kind Regards
David
16 Comments
Mathieu NOE
on 3 Jun 2021
hello
so those signals are supposed to represent a cos and a sin of an angle ?
what is the amplitude scale factor , because icos and sin are supposed to be within -1 / +1 range
you can use the function atan2 to get the angle from its cos and sin values :
help atan2
atan2 Four quadrant inverse tangent.
atan2(Y,X) is the four quadrant arctangent of the elements of X and Y
such that -pi <= atan2(Y,X) <= pi. X and Y must have compatible sizes.
In the simplest cases, they can be the same size or one can be a
scalar. Two inputs have compatible sizes if, for every dimension, the
dimension sizes of the inputs are either the same or one of them is 1.
David Jones
on 4 Jun 2021
Hi Mathieu
Thank you for getting back to me again.
In answer to your question the amplitude scale factor is bits I have not converted them to anything yet. Sorry I dont understand how to do this can you please show me a phase plot in Degrees? I have incorperated this line of code
phase_data = atan2(cosine_data,sine_data); but on a plot just swings between -3 and +3 I have attached the latest data file.
Thank you for any help you can offer
David
Mathieu NOE
on 4 Jun 2021
hello David
FYI , atan2 needs Y,X as arguments (in this order) and not X,Y, so the sine component is the first arguement
second the angle computation is not affected by the scale factor you can apply on X, Y , so even if your data scale is +/-1024 and not +/- 1 , it does not change the angle
phase_data = atan2(raw_ad_data_sine/1024,raw_ad_data_cosine/1024); % NB arguments : sin, cos
phase_data = atan2(raw_ad_data_sine,raw_ad_data_cosine); % gives the same result
figure,plot(phase_data)
David Jones
on 4 Jun 2021
HI Mathieu
Okay I have achieved the same results but I dont understand why it is limited to +3 or -3 I have also run a sample which is attached with no breathing the results are still + or -3 can you please help me to understand as this is unusable why is there so musch phase shift when there is no breathing.
Thank You
David
Bjorn Gustavsson
on 4 Jun 2021
It is not limited to -3 - +3, it is limited to .
As to the question about why you have the signal you have: it is very difficult to speculate because there is not enough information about the source of your signal.
Mathieu NOE
on 4 Jun 2021
yes maybe some explanation about how those two signals were generated / recorded might help
David Jones
on 4 Jun 2021
Hi Mathieu
These 2 signals are generated using a lock in amplifier and detect when a patient is breathing or not as you have seen by the difference in the amplitude of just the sine channel, but it seems it does not do the same in phase I believe you can get the amplitude of the 2 signals using Sqare root (x^2 + y^2) is this correct? Maybe I could put in a low pass to take out some of the noise on the phase side, any help would be appreciated.
Kind Regards
David
Mathieu NOE
on 4 Jun 2021
David
so the two siganls are the sine and cosine components of a signal modulated by a carrier; although this is quite obvious for fixed frequency carriers (like AM modulation) , I'm surprised it is used in breathing signal analysis - where do you get a carrier that is "locked" to the breathing actvity ? or is the carrier frequency fixed at a given value (expected breathing frequency ?)
so we can do a lot of post processing here but I wonder what this phase is telling about the breathing itself , or if the sine / cosine amplitude fluctuation is simply because there is always a frequency mismatch between the carrier and the physical signal that enters your amplifier
for sure we can take the rms amplitude (Sqare root (x^2 + y^2)) and do a bit of smoothing if needed , but as you have seen in the plot before, the angle output is not very noisy , and I believe this is also because you smoothed the sine / cosine data (vs the first file you posted)
David Jones
on 7 Jun 2021
Hi Mathieu
The carrier is produced outside and the only thing we see inside matlab is the Sine and cosine filtered in the electronics low pass 0.1 Hertz and 0.4 hight pass however I am surprised by the calculation of the phase noise would an FFT using angle be any better at detecting the phase? .
I am just experimenting with the phase (or any other method) to see if it can improved the resolution of breathing.
Kind Regards
David
Mathieu NOE
on 7 Jun 2021
hello David
ok thanks for the info , but I wonder how I can furter assist you;
also , in case of "no" breathing activity, what would the signals look like ? is there any risk that the phase computaion can be completely misleading as you have only low level signal + a bit of noise ?
Bjorn Gustavsson
on 7 Jun 2021
@David Jones, One thing you have to do with the phase as calculated is to unwrap it - the 2*pi jumps you have are "unphysical" (not exactly unmathematical but nontheless nonsensical). After doing that you might see something. Though it seems to me that your mixing-frequencies seems rather close to the breath-frequencies?
David Jones
on 8 Jun 2021
Hi
Thanks for responding I think after many experments that the phase gives no more than the amplitude response but thanks for all the help.
I do have one last request, can any one show me how do make a 60 second buffer for 6000 samples and basicaly add to the start of the buffer evey n minutes with 10 seconds of new samples and remove the oldest 10 seconds of samples.
Thank you for any help
David
David Jones
on 9 Jun 2021
Hi
I have looked up Phase Unrapping and also DACM algorithm, can any one show me how to do this.
Any help would be greatly appreciated.
David
David Jones
on 10 Jun 2021
Hi
Yes it does, I had never used it before, but after testing I think there is to much small phase noise to be able to use the phase.
Thanks
David
Answers (1)
Bjorn Gustavsson
on 10 Jun 2021
Does something like this extract variations that are related to the breathing-pattern:
phi_u = unwrap(atan2(raw_ad_data_sine,raw_ad_data_cosine));
fK = hanning(1001); % Arbitrary choise of low-pass filter
fK = fK/sum(fK);
subplot(2,1,1)
plot(t,phi_u) % Raw unwrapped phase
hold on
plot(t,filtfilt(fK,1,phi_u)) % low-pass-filtered phase
subplot(2,1,2)
plot(t,phi_u-filtfilt(fK,1,phi_u)) % unwrapped phase with slow variations removed.
Since we dont know what signal to extract it is difficult to advice. My gues is that your mixing signals are too close to the breath-pattern in frequency.
1 Comment
David Jones
on 10 Jun 2021
Hi Bjorn
Thanks for getting back I have attached the sample data the signals of interest are 0.1 hertz to 0.45 hertz the mixer frequency is in the Mega Hertz, I then filter in the electronics the sine and cosine as above, I have also attacted the amplitude output from Matlab, can I ask you please to take a look and le me know your thoughts
Thank you
David
See Also
Categories
Find more on Pulsed Waveforms in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)