How to workaround this problem with angle? (2024)

8 views (last 30 days)

Show older comments

SIMONE on 4 Mar 2024

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle

Edited: SIMONE on 5 Mar 2024

Open in MATLAB Online

(I started with MATLAB today, so I'm new). I created a script to calculate the fourier transform of a function, the amplitude spectrum and the phase spectrum. For testing purposes i tried calculating using as the input function the rectangular impulse. The fourier transform and the amplitude spectrum are right, the phase spectrum is not. When i use the angle function or the atan2 to calculate the angle i get as output alternating +pi and -pi where the output should be +pi. Watching the inputs and outputs of the angle function everything seems fine, the angle function works as intended. I think that the problem is related to the sign of the imaginary part, and maybe to the fact that they are really small number (e^-18) . I know that the code is all based on an approximation so it could be normal but i want to know if there is a workaround. (I know there is already a function(fft))

%total range of omega

delta_omega=20;

%intervals of omega that are used to approximate

omega_precision=0.01;

%initializatoin of the results vector

res=0:omega_precision:delta_omega;

%all the values of omega

val_omega=-delta_omega/2:omega_precision:delta_omega/2;

%for every omega calculate it's fourier transform (from -delta_omega/2 to delta_omega/2)

for n=1:delta_omega/omega_precision+1

res(n)=X((n-(delta_omega/omega_precision/2))*omega_precision);

end

%plot of the real value of the fourier tranform

plot(val_omega,real(res),"red");

hold on;

%plot og the immaginary values of the fourier tranform

plot(val_omega,imag(res),"cyan");

%amplitude spectrum values

V=abs(res)/pi;

hold on;

%plot of the amplitude spectrum

plot(val_omega,V,"green");

xlabel("omega");

%phase spectrum values

Fi=-angle(res);

%plot of the phase spectrumm values

plot(val_omega,Fi,"blue")

How to workaround this problem with angle? (2)

%fourier transform of function x(t)

function X=X(omega)

handle= @(t) x(t).*exp(-1i*omega*t);

X=integral(handle,-1100,1100);

end

%function x(t) (rectangular impulse)

function x = x(t)

%amplitude

A=1;

tao=pi;

x=1:length(tao);

for j=1:length(t)

if(t(j)<=tao/2 && t(j)>=-tao/2)

x(j)=A;

else

x(j)=0;

end

end

end

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (2)

Chunru on 5 Mar 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle#answer_1421021

Open in MATLAB Online

You can try unwrap function.

%total range of omega

delta_omega=20;

%intervals of omega that are used to approximate

omega_precision=0.01;

%initializatoin of the results vector

res=0:omega_precision:delta_omega;

%all the values of omega

val_omega=-delta_omega/2:omega_precision:delta_omega/2;

%for every omega calculate it's fourier transform (from -delta_omega/2 to delta_omega/2)

for n=1:delta_omega/omega_precision+1

res(n)=X((n-(delta_omega/omega_precision/2))*omega_precision);

end

%plot of the real value of the fourier tranform

plot(val_omega,real(res),"red");

hold on;

%plot og the immaginary values of the fourier tranform

plot(val_omega,imag(res),"cyan");

%amplitude spectrum values

V=abs(res)/pi;

hold on;

%plot of the amplitude spectrum

plot(val_omega,V,"green");

xlabel("omega");

%phase spectrum values

Fi=-angle(res);

%plot of the phase spectrumm values

% plot(val_omega,Fi,"blue")

plot(val_omega,unwrap(Fi),"blue")

How to workaround this problem with angle? (4)

%fourier transform of function x(t)

function X=X(omega)

handle= @(t) x(t).*exp(-1i*omega*t);

X=integral(handle,-1100,1100);

end

%function x(t) (rectangular impulse)

function x = x(t)

%amplitude

A=1;

tao=pi;

x=1:length(tao);

for j=1:length(t)

if(t(j)<=tao/2 && t(j)>=-tao/2)

x(j)=A;

else

x(j)=0;

end

end

end

1 Comment

Show -1 older commentsHide -1 older comments

SIMONE on 5 Mar 2024

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle#comment_3088841

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle#comment_3088841

Edited: SIMONE on 5 Mar 2024

I have 2 problems:

1) I already tried to unse unwrap and my output was different from yours, does some know why it looke like this when i run it?

How to workaround this problem with angle? (6)

2) This would be wrong, maybe it's my fault and i did something else wrong, as the phase spectrum should look like this from what i found online:

How to workaround this problem with angle? (7)

Thanks for rsponding and trying ti help me

Sign in to comment.

VBBV on 5 Mar 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle#answer_1421031

Open in MATLAB Online

You have real and imaginary componens for variable Fi, so use only real angles

%total range of omega

delta_omega=20;

%intervals of omega that are used to approximate

omega_precision=0.01;

%initializatoin of the results vector

res=0:omega_precision:delta_omega;

%all the values of omega

val_omega=-delta_omega/2:omega_precision:delta_omega/2;

%for every omega calculate it's fourier transform (from -delta_omega/2 to delta_omega/2)

for n=1:delta_omega/omega_precision+1

res(n)=X((n-(delta_omega/omega_precision/2))*omega_precision);

end

%plot of the real value of the fourier tranform

plot(val_omega,real(res),"red");

hold on;

%plot og the immaginary values of the fourier tranform

plot(val_omega,imag(res),"cyan");

%amplitude spectrum values

V=abs(res)/pi;

hold on;

%plot of the amplitude spectrum

plot(val_omega,V,"green");

xlabel("omega");

%phase spectrum values

Fi=angle(real(res));

%plot of the phase spectrumm values

plot(val_omega,Fi,"blue")

How to workaround this problem with angle? (9)

%fourier transform of function x(t)

function X=X(omega)

handle= @(t) x(t).*exp(-1i*omega*t);

X=integral(handle,-1100,1100);

end

%function x(t) (rectangular impulse)

function x = x(t)

%amplitude

A=1;

tao=pi;

%x=1:length(tao);

for j=1:length(t)

if(t(j)<=tao/2 & t(j)>=-tao/2)

x(j)=A;

else

x(j)=0;

end

end

end

2 Comments

Show NoneHide None

SIMONE on 5 Mar 2024

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle#comment_3088646

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle#comment_3088646

Open in MATLAB Online

This would work for this specific function x(t), another solution for that would be abs(angle(res)). The problem is that it is not generalized, if i try using a function x(t) that has the imaginary part in the fourier transform then the phase spectrum is wrong. An example is using x(t)={A*exp(-abs(t)/t_0) for t>=0 , 0 for t<0}. With this funtion -angle(res) gives the right phase spectrum and angle(real(res)) equals 0. Thanks for your response and your time.

function x = x(t)

%amplitude

A=1;

t_0=1;

for j=1:length(t)

if t(j)>=0

x(j)=A*exp(-abs(t(j))/t_0);

else

x(j)=0;

end

end

end

VBBV on 5 Mar 2024

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle#comment_3088791

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2090331-how-to-workaround-this-problem-with-angle#comment_3088791

Open in MATLAB Online

If you consider the imaginary part of Fourier spectrum, then use the imag(res) . For a real periodic signal, the phase information of imaginary component of signal has no significance

%total range of omega

delta_omega=20;

%intervals of omega that are used to approximate

omega_precision=0.01;

%initializatoin of the results vector

res=0:omega_precision:delta_omega;

%all the values of omega

val_omega=-delta_omega/2:omega_precision:delta_omega/2;

%for every omega calculate it's fourier transform (from -delta_omega/2 to delta_omega/2)

for n=1:delta_omega/omega_precision+1

res(n)=X((n-(delta_omega/omega_precision/2))*omega_precision);

end

%plot of the real value of the fourier tranform

plot(val_omega,real(res),"red");

hold on;

%plot og the immaginary values of the fourier tranform

plot(val_omega,imag(res),"cyan");

%amplitude spectrum values

V=abs(res)/pi;

hold on;

%plot of the amplitude spectrum

plot(val_omega,V,"green");

xlabel("omega");

%phase spectrum values

Fi= angle(imag(res));

%plot of the phase spectrumm values

plot(val_omega,Fi,"blue")

How to workaround this problem with angle? (12)

%fourier transform of function x(t)

function X=X(omega)

handle= @(t) x(t).*exp(-1i*omega*t);

X=integral(handle,-1100,1100);

end

%function x(t) (rectangular impulse)

function x = x(t)

%amplitude

A=1;

t_0=1;

for j=1:length(t)

if t(j)>=0

x(j)=A*exp(-abs(t(j))/t_0);

else

x(j)=0;

end

end

end

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Signal ProcessingSignal Processing ToolboxDigital and Analog FiltersMultirate Signal Processing

Find more on Multirate Signal Processing in Help Center and File Exchange

Tags

  • fourier-transform
  • matlab
  • signal

Products

  • MATLAB

Release

R2023b

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.


How to workaround this problem with angle? (13)

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

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
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to workaround this problem with angle? (2024)

References

Top Articles
Parent Portal Pat Med
Brookshire Brothers Pay Portal
James Earl Jones: George Lucas and Mark Hamill lead tributes to actor
Happel Real Estate
Huggies Size 4 Walgreens
Wcco Crime News
Vons Credit Union Routing Number
211475039
Denman Promo Code
Understanding Filmyzilla - A Comprehensive Guide to Movies
Circle L Bassets
8776685260
A Qué Hora Cierran Spectrum
50 budget recipes to feed a large crowd
Dr Frita Mcrae Fisher Husband
Feliz Domingo Bendiciones, Mensajes cristianos para compartir | Todo imágenes
Best Fantasy Basketball Team
Chesapeake Wv Topix
Craigslist Com Humboldt
Www.patientnotebook.com/Prima
Dd Codeshare
Thomas Funeral Home Sparta Nc
Gdp E239 Bts
159 Joseph St, East Brunswick Township, NJ 08816 - MLS 2503534R - Coldwell Banker
How 'The Jordan Rules' inspired template for Raiders' 'Mahomes Rules'
Maximise Your Funding: Key Insights on Accounting for Grants
Wells Fargo Banks In Florida
Oklahoma City Municipal Courthouse
April 7 Final Jeopardy
Used Golf Clubs On Craigslist
Hendricks County Mugshots Busted Newspaper
Decree Of Spite Poe
Kristian Andersen | Scripps Research
Oh The Pawsibilities Salon & Stay Plano
Usc Human Biology
Trade Chart Dave Richard
Syracuse Deadline
Dawson Myers Fairview Nc
Official Klj
On-Campus Student Employment
Bridger Elementary Logan
Montefiore Email Outlook Login
My Scheduler Hca Cloud
Cranes for sale - used and new - TrucksNL
Gtl Visit Me Alameda
Craigslist Free Stuff Columbus Ga
'We weren't done': Spacebar Arcade closes its doors for good
Desi Cinemas.com
Magnifeye Alcon
Best Asian Bb Cream For Oily Skin
Santa Rosa Craigslist Free Stuff
Cb2 South Coast Plaza
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 6078

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.