Class: SpectralFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/spectralfilter.rb

Overview

Spectral analysis and filtering

project page: rubyforge.org/projects/extcsv

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ SpectralFilter

x and y can be Arrays of Floats or GSL:Vectors. The sampling attribute should be checked and changed carefully if necessary.



11
12
13
14
15
# File 'lib/spectralfilter.rb', line 11

def initialize(x, y)
  @x, @y    = [x,y].collect {|v| v.kind_of?(Array) ? GSL::Vector.alloc(v) : v}
  @sampling = ((@x[-1]-@x[0])/@x.size)**(-1)
  @fft      = @y.fft
end

Instance Attribute Details

#fftObject

Returns the value of attribute fft.



7
8
9
# File 'lib/spectralfilter.rb', line 7

def fft
  @fft
end

#samplingObject

Returns the value of attribute sampling.



7
8
9
# File 'lib/spectralfilter.rb', line 7

def sampling
  @sampling
end

#xObject

Returns the value of attribute x.



7
8
9
# File 'lib/spectralfilter.rb', line 7

def x
  @x
end

#yObject

Returns the value of attribute y.



7
8
9
# File 'lib/spectralfilter.rb', line 7

def y
  @y
end

Instance Method Details

#bandblock(freqMin, freqMax) ⇒ Object

Frequences in the range between freqMin and freqMax are omitted



39
40
41
42
43
44
45
# File 'lib/spectralfilter.rb', line 39

def bandblock(freqMin,freqMax)
  n = @y.size
  (0...n).each {|i| 
    freq_   = i*(0.5/(n*(@x[1]-@x[0])))
    @fft[i] = 0 if !(freq_ < freqMin or freq_ > freqMax)
  }
end

#bandpass(freqMin, freqMax) ⇒ Object

Frequences outside the range between freqMin and freqMax are subpressed



30
31
32
33
34
35
36
# File 'lib/spectralfilter.rb', line 30

def bandpass(freqMin,freqMax)
  n = @y.size
  (0...n).each {|i| 
    freq_   = i*(0.5/(n*(@x[1]-@x[0])))
    @fft[i] = 0 if (freq_ < freqMin or freq_ > freqMax)
  }
end

#highpass(freq) ⇒ Object

Frequences smaller than freq are omitted



24
25
26
27
# File 'lib/spectralfilter.rb', line 24

def highpass(freq)
  n = @y.size
  (0...n).each {|i| @fft[i] = 0 if i*(0.5/(n*(@x[1]-@x[0]))) < freq}
end

#lowpass(freq) ⇒ Object

Frequences larger than freq are omitted



18
19
20
21
# File 'lib/spectralfilter.rb', line 18

def lowpass(freq)
  n = @y.size
  (0...n).each {|i| @fft[i] = 0 if i*(0.5/(n*(@x[1]-@x[0]))) > freq}
end

#plotData(opts = "-C -g 3") ⇒ Object

Display Datasets before and aftern Filtering



59
60
61
62
# File 'lib/spectralfilter.rb', line 59

def plotData(opts="-C -g 3")
  GSL::graph(@x,@y,           opts  + " -L 'Original Data'")
  GSL::graph(@x,@fft.inverse, opts + " -L 'After Filtering'")
end

#plotSpec(opts = "-C -g 3 -x 0 #{@sampling/2} -X 'Frequency [Hz]'") ⇒ Object

Display the spectrum



53
54
55
56
# File 'lib/spectralfilter.rb', line 53

def plotSpec(opts="-C -g 3 -x 0 #{@sampling/2} -X 'Frequency [Hz]'")
  mag, phase, frq = proc4plot
  GSL::graph(frq, mag, opts)
end

#renewObject

reset the FFT to the initial state



48
49
50
# File 'lib/spectralfilter.rb', line 48

def renew
  @fft = @y.fft
end