File: complexFFT.rb

package info (click to toggle)
ruby-fftw3 1.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 160 kB
  • sloc: ansic: 501; ruby: 203; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,510 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require "narray"  # This line is needed for rake test when making a gem package.
require "numru/fftw3"
require "test/unit"
include NumRu

class FFTW3Test < Test::Unit::TestCase
  def setup
    @eps = 1e-10
    @seps = 1e-6
  end

  def test_fft_fw_bk
    na = NArray.float(8,4).fill(1)  # will be corced to complex
    na[1,1]=5
    fc = FFTW3.fft_fw(na)
    nb = FFTW3.fft_bk(fc).real
    assert( (na-nb).abs.max < @eps )

    fc = FFTW3.fft_fw(na,1)
    nb = FFTW3.fft_bk(fc,1).real
    assert( (na-nb).abs.max < @eps )
  end

  def test_real_all_dims
    na = NArray.float(8,4).fill(1)  # will be corced to complex
    na[1,1]=5
    fc = FFTW3.fft(na, FFTW3::FORWARD)/na.length 
    nb = FFTW3.fft(fc, FFTW3::BACKWARD).real
    assert( (na-nb).abs.max < @eps )
  end

  def test_complex_all_dims
    na = NArray.complex(8,4).fill(1) * Complex::I
    na[1,1]=5
    fc = FFTW3.fft(na, -1)/na.length 
    nb = FFTW3.fft(fc, 1)
    assert( (na-nb).abs.max < @eps )
  end

  def test_dim_selection
    na = NArray.float(8,4).indgen!
    fc = FFTW3.fft(na, FFTW3::FORWARD, 0)
    fc = FFTW3.fft(fc, FFTW3::FORWARD, 1)
    fc2 = FFTW3.fft(na, FFTW3::FORWARD)
    assert( (fc-fc2).abs.max < @eps )
  end

  # TEST: single float (treated as single if lib fftw3f exits).
  # see http://www.fftw.org/fftw3_doc/Precision.html for more info
  def test_single_float
    na = NArray.sfloat(8,4).indgen!
    fc = FFTW3.fft(na, -1)/na.length 
    nb = FFTW3.fft(fc, 1).real
    assert( (na-nb).abs.max < @seps )
  end
end