question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doubt about behavior of `dft`

See original GitHub issue

Great thing that you guys have added inverse ffts! However, I can’t figure out how to exactly preserve the original coordinates of the data though. I was able to do that with ffts so I’m guessing there should be also a way to do this with xrft. Here’s a MWE:

In [105]: import xarray as xr

In [106]: import xrft

In [107]: T = xr.tutorial.open_dataset("air_temperature").air.isel(time=[0, 1])

In [108]: T_f = xrft.dft(T, dim="lat", true_phase=True, true_amplitude=True)

In [109]: T2 = xrft.idft(T_f, dim="freq_lat", true_phase=True, true_amplitude=True,)

In [110]: T.lat
Out[110]: 
<xarray.DataArray 'lat' (lat: 25)>
array([75. , 72.5, 70. , 67.5, 65. , 62.5, 60. , 57.5, 55. , 52.5, 50. , 47.5,
       45. , 42.5, 40. , 37.5, 35. , 32.5, 30. , 27.5, 25. , 22.5, 20. , 17.5,
       15. ], dtype=float32)
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
Attributes:
    standard_name:  latitude
    long_name:      Latitude
    units:          degrees_north
    axis:           Y

In [111]: T2.lat
Out[111]: 
<xarray.DataArray 'lat' (lat: 25)>
array([-30. , -27.5, -25. , -22.5, -20. , -17.5, -15. , -12.5, -10. ,  -7.5,
        -5. ,  -2.5,   0. ,   2.5,   5. ,   7.5,  10. ,  12.5,  15. ,  17.5,
        20. ,  22.5,  25. ,  27.5,  30. ])
Coordinates:
  * lat      (lat) float64 -30.0 -27.5 -25.0 -22.5 -20.0 ... 22.5 25.0 27.5 30.0
Attributes:
    spacing:  2.4999999999999964

Using the xrft.fft option also doesn’t produce what I expected:

In [112]: T_f = xrft.fft(T, dim="lat",)

In [113]: T2 = xrft.ifft(T_f, dim="freq_lat")

In [114]: T.lat
Out[114]: 
<xarray.DataArray 'lat' (lat: 25)>
array([75. , 72.5, 70. , 67.5, 65. , 62.5, 60. , 57.5, 55. , 52.5, 50. , 47.5,
       45. , 42.5, 40. , 37.5, 35. , 32.5, 30. , 27.5, 25. , 22.5, 20. , 17.5,
       15. ], dtype=float32)
Coordinates:
  * lat      (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
Attributes:
    standard_name:  latitude
    long_name:      Latitude
    units:          degrees_north
    axis:           Y

In [115]: T2.lat
Out[115]: 
<xarray.DataArray 'lat' (lat: 25)>
array([-30. , -27.5, -25. , -22.5, -20. , -17.5, -15. , -12.5, -10. ,  -7.5,
        -5. ,  -2.5,   0. ,   2.5,   5. ,   7.5,  10. ,  12.5,  15. ,  17.5,
        20. ,  22.5,  25. ,  27.5,  30. ])
Coordinates:
  * lat      (lat) float64 -30.0 -27.5 -25.0 -22.5 -20.0 ... 22.5 25.0 27.5 30.0
Attributes:
    spacing:  2.4999999999999964

I feel like I’m missing something obvious but can’t quite figure it out…

How can I preserve coords so that I get the same result going to fourier space and back?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
lanouguecommented, Apr 19, 2021

Hi @tomchor, @roxyboy ,

When T_f(…,true_phase=True) is evaluated, the phase is preserved but the coordinates of T are lost in the transformation. T_f is in the Fourier domain and is not associated to any direct (lat) coordinates. Thus, when idft() is called on T_f, idft has no idea on which range of coordinates you need the result. The default is on centered coordinates with the same range of your inital coordinates ([-30,30] in your example). If you want your data back in the good range of coordiantes, you need to shift the centered standard output coordinates. As your initial coordinates are around 45 degree, you have to call idft with the lag argument: T2 = xrft.idft(T_f, dim=“freq_lat”, true_phase=True, true_amplitude=True, lag=45)

However, looking to you example, I see that the returned result is the same as the orignal T but reversed. This is because signal is on decreasing “lat” axis. This is a xrft bug ! For now, as a workaround, use: T = T.sortby(‘lat’) just after loading T to get the expected behaviour.

1reaction
lanouguecommented, May 20, 2021

Yes, it should resolve this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unstable behavior of Discrete Fourier Transform and Fourier ...
One issue with your argument is that you consider only the real part of the DFT (why?). You might as well consider only...
Read more >
Challenges for Density Functional Theory | Chemical Reviews
This brings us to another very interesting question raised by the performance of DFT, as highlighted by large errors for one-electron systems.
Read more >
DFT - DSPRelated.com
I would like to get some concepts cleared on DFT. I will use a single tone signal (say of frequency f0) in time...
Read more >
Why is DFT periodic? Why is Continuous time FT not ... - Reddit
There's some good answers here, so want to take a different tack and explain it from a transform perspective.
Read more >
Scientists doubt that DeepMind's AI is as good for fractional ...
But there are well-known circumstances where DFT tools fail. One is predicting how atoms share electrons; in one famous example, DFT methods ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found