Weird (wrong?) result for fft

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Weird (wrong?) result for fft

Filippo Monari
Hi all,

I am plying around with fft function from the stats package.
Running the example that is listed in the documentation:

x <- 1:4
fft(x) #output 1
fft(fft(x), inverse = TRUE)/length(x) #output 2

I was expecting that output 1 and two were the same but I get:

> fft(x)
[1] 10+0i -2+2i -2+0i -2-2i

> ft(fft(x), inverse = TRUE)/length(x)
[1] 1+0i 2+0i 3+0i 4+0i

Am I doing something wrong or is there a bug somewhere?

Regards,
Filippo

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Weird (wrong?) result for fft

Jeff Newmiller
Neither. The discrete Fourier transform is a complex number operation. R-help is per the Posting Guide not an appropriate place to discuss theory in depth, and there is plenty of theory in this question and practically no R, but you can examine your result more closely with the functions described in ?complex.

On March 22, 2021 9:17:11 AM PDT, Filippo Monari <[hidden email]> wrote:

>Hi all,
>
>I am plying around with fft function from the stats package.
>Running the example that is listed in the documentation:
>
>x <- 1:4
>fft(x) #output 1
>fft(fft(x), inverse = TRUE)/length(x) #output 2
>
>I was expecting that output 1 and two were the same but I get:
>
>> fft(x)
>[1] 10+0i -2+2i -2+0i -2-2i
>
>> ft(fft(x), inverse = TRUE)/length(x)
>[1] 1+0i 2+0i 3+0i 4+0i
>
>Am I doing something wrong or is there a bug somewhere?
>
>Regards,
>Filippo
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Weird (wrong?) result for fft

Jiefei Wang
In reply to this post by Filippo Monari
Hi Filippo,

Why do you expect these two expressions to produce the same output?
Especially the second expression contains the first expression. There is no
way to have the same result unless the function "fft( * , inverse = TRUE)"
does nothing but multiplies the input by the input's length. It does not
make any sense.

Actually, both results are correct as Jeff said. The first is the result of
discrete Fourier transform and the second is the reverse of the transform.
That's why the second output is the same as your variable "x". There are
some rounding errors in the computation so you might get very
small imaginary numbers after doing the reverse transformation. That's why
you see "0i" in the second output.

Best,
Jiefei

On Tue, Mar 23, 2021 at 12:17 AM Filippo Monari <[hidden email]> wrote:

> Hi all,
>
> I am plying around with fft function from the stats package.
> Running the example that is listed in the documentation:
>
> x <- 1:4
> fft(x) #output 1
> fft(fft(x), inverse = TRUE)/length(x) #output 2
>
> I was expecting that output 1 and two were the same but I get:
>
> > fft(x)
> [1] 10+0i -2+2i -2+0i -2-2i
>
> > ft(fft(x), inverse = TRUE)/length(x)
> [1] 1+0i 2+0i 3+0i 4+0i
>
> Am I doing something wrong or is there a bug somewhere?
>
> Regards,
> Filippo
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.