# What scaling applies to the irfft (inverse fast fourier transform - FFTW.jl package)

**URL:** https://discourse.julialang.org/t/what-scaling-applies-to-the-irfft-inverse-fast-fourier-transform-fftw-jl-package/47476
**Category:** Signal and Image Processing
**Tags:** fftw
**Created:** [September 29, 2020, 8:35am UTC](https://discourse.julialang.org/t/what-scaling-applies-to-the-irfft-inverse-fast-fourier-transform-fftw-jl-package/47476 "2020-09-29T08:35:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)
#### Post date: [September 29, 2020, 8:35am UTC](https://discourse.julialang.org/t/what-scaling-applies-to-the-irfft-inverse-fast-fourier-transform-fftw-jl-package/47476/1 "2020-09-29T08:35:17Z")

</div>

I am trying to understand scaling of the irfft function of the FFTW.jl package. I have a function that defines a continuous frequency domain signal (in radians). The complex portion of the signal has odd symmetry and the real part of the signal has even symmetry. I sample the positive half of the frequency domain signal and end up with an array of N elements. I then apply the irfft() function to the signal. I get the right shape out, but it is not scaled properly.

1. Can anyone advise me on the scaling factors that are applicable to this function?
2. Would it also be necessary to convert frequency domain signal to Hertz rather than radians before applying the irfft()?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 29, 2020, 12:30pm UTC](https://discourse.julialang.org/t/what-scaling-applies-to-the-irfft-inverse-fast-fourier-transform-fftw-jl-package/47476/2 "2020-09-29T12:30:46Z")

</div>

> [@jleman](#):
>
> Can anyone advise me on the scaling factors that are applicable to this function?

It performs the “backward” transform [defined here](http://fftw.org/fftw3_doc/The-1d-Real_002ddata-DFT.html#The-1d-Real_002ddata-DFT) multiplied by a scale factor of 1/n where n is the length of the real output array, so that it is the inverse of `rfft`.

> [@jleman](#):
>
> Would it also be necessary to convert frequency domain signal to Hertz rather than radians before applying the irfft()?

No. The frequencies are not an input to a DFT, and it is a linear operation so that it doesn’t matter what scaling/units you use.

---

<div class="post-metadata">

### Author: ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)
#### Post date: [September 29, 2020, 4:42pm UTC](https://discourse.julialang.org/t/what-scaling-applies-to-the-irfft-inverse-fast-fourier-transform-fftw-jl-package/47476/3 "2020-09-29T16:42:01Z")

</div>

Ok, so does the sampling interval in the frequency domain affect the scaling? In the example below, I have a fixed number of samples, `N`. When I change `ωmax` (the maximum frequency in the range), the amplitude of the ifft result changes even though `N` does not change. The plots below are for `ωmax=4` and `ωmax=20` respectively. The time domain signal amplitude seems inversely related to the frequency sampling interval, despite N remaining constant. To recover the correct time domain signal amplitude it seems I need to include some other scaling factor related to frequency.

```julia
using FFTW
using SpecialFunctions
using Gaston #plotting package

#function to generate a frequency domain signal
function K0diff(ω,r,r′,z)
    if ω == 0
        result = log(r′/r) + 0im      
    else
        result = exp(1im*ω*z)*(besselk(0,abs(ω)*r) - besselk(0,abs(ω)*r′))
    end
    return result
end

#create a frequency domain function and apply irfft
N = 2^10
Ndiv2 = Int(floor(N/2))
ωmax = 4
ω = range(-ωmax,ωmax,length = N + 1)
frequencyDomainSignal = K0diff.(ω,5,20,1)

p = plan_irfft(frequencyDomainSignal[Ndiv2+1:end],N,1)
timeDomainSignal = fftshift(p*frequencyDomainSignal[Ndiv2+1:end],1)

#output plots using Gaston
p1 = plot(real.(frequencyDomainSignal),Axes(xlabel = "'Frequency Index'",ylabel = "'Amplitude'"), handle = 1)
plot!(imag.(frequencyDomainSignal))
p2 = plot(timeDomainSignal,Axes(xlabel = "'Time Index'",ylabel = "'Amplitude'"), handle = 2)
plot([p1 ; p2])

```

with `ωmax=4`

 ![plotw4](https://global.discourse-cdn.com/julialang/original/3X/6/7/67af08a4ec3fcb2a9857c4ef455212d094fc860f.jpeg)

with `ωmax=20`

 ![plotw20](https://global.discourse-cdn.com/julialang/original/3X/f/0/f0778c23c2abcdf5d9657bb0d5cc1c48fd65c151.jpeg)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 29, 2020, 4:58pm UTC](https://discourse.julialang.org/t/what-scaling-applies-to-the-irfft-inverse-fast-fourier-transform-fftw-jl-package/47476/4 "2020-09-29T16:58:08Z")

</div>

> [@jleman](#):
>
> Ok, so does the sampling interval in the frequency domain affect the scaling?

If you are thinking of the DFT as an approximation for a continuous Fourier transform, then you should multiply it by Δω in order for the sum to be an approximation for \int d\omega. (The finite window is also an approximation.)

This has nothing to do with Julia or the FFTW.jl package. Any FFT function computes the discrete Fourier transform (DFT), _not_ the continuous Fourier transform, and you need to understand how the two relate for your application.

---

<div class="post-metadata">

### Author: ![jleman](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jleman](https://discourse.julialang.org/u/jleman)
#### Post date: [September 29, 2020, 5:28pm UTC](https://discourse.julialang.org/t/what-scaling-applies-to-the-irfft-inverse-fast-fourier-transform-fftw-jl-package/47476/5 "2020-09-29T17:28:37Z")

</div>

Ok, that makes sense and fixed my problem. DSP related functions are outside my comfort zone and there is so much information available. I was barking up the wrong tree in my research. Thanks for the redirect.
