spectralfilter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2009, Ralf Mueller (stark.dreamdetective@googlemail.com)
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+
14
+ * The names of its contributors may not be used to endorse or promote
15
+ products derived from this software without specific prior written
16
+ permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/gemspec ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = "spectralfilter"
5
+ s.version = "0.0.1"
6
+ s.author = "Ralf M�ller"
7
+ s.email = "stark.dreamdetective@gmail.com"
8
+ s.homepage = "http://rubyforge.org/projects/extcsv"
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = "Filtering Datasets via Highpass, Lowpass, Bandpass or Bandblock"
11
+ candidates = Dir.glob("lib/*.rb") + [ "rakefile", "gemspec","LICENSE"]
12
+ s.files = candidates.delete_if do |item|
13
+ item.include?("pkg") || item.include?("www") || item.include?("rdoc")
14
+ end
15
+ s.require_path = "lib"
16
+ s.test_files = Dir.glob("test/test_*.rb")
17
+ s.has_rdoc = true
18
+ end
19
+
20
+ # vim:ft=ruby
@@ -0,0 +1,73 @@
1
+ require 'rbgsl'
2
+ require 'mathn'
3
+
4
+ class SpectralFilter
5
+ attr_accessor :x,:y,:fft,:sampling
6
+
7
+ # x and y can be Arrays of Floats or GSL:Vectors. The sampling attribute
8
+ # should be checked and changed carefully if necessary.
9
+ def initialize(x, y)
10
+ @x, @y = [x,y].collect {|v| v.kind_of?(Array) ? GSL::Vector.alloc(v) : v}
11
+ @sampling = ((@x[-1]-@x[0])/@x.size)**(-1)
12
+ @fft = @y.fft
13
+ end
14
+
15
+ # Frequences larger than freq are omitted
16
+ def lowpass(freq)
17
+ n=@y.size
18
+ (0...n).each {|i| @fft[i] = 0 if i*(0.5/(n*(@x[1]-@x[0]))) > freq}
19
+ end
20
+
21
+ # Frequences smaller than freq are omitted
22
+ def highpass(freq)
23
+ n=@y.size
24
+ (0...n).each {|i| @fft[i] = 0 if i*(0.5/(n*(@x[1]-@x[0]))) < freq}
25
+ end
26
+
27
+ # Frequences outside the range between freqMin and freqMax are subpressed
28
+ def bandpass(freqMin,freqMax)
29
+ n=@y.size
30
+ (0...n).each {|i|
31
+ freq_ = i*(0.5/(n*(@x[1]-@x[0])))
32
+ @fft[i] = 0 if (freq_ < freqMin or freq_ > freqMax)
33
+ }
34
+ end
35
+
36
+ # Frequences in the range between freqMin and freqMax are omitted
37
+ def bandblock(freqMin,freqMax)
38
+ n=@y.size
39
+ (0...n).each {|i|
40
+ freq_ = i*(0.5/(n*(@x[1]-@x[0])))
41
+ @fft[i] = 0 if !(freq_ < freqMin or freq_ > freqMax)
42
+ }
43
+ end
44
+
45
+ # reset the FFT to the initial state
46
+ def renew
47
+ @fft = @y.fft
48
+ end
49
+
50
+ # Display the spectrum
51
+ def plotSpec(opts="-C -g 3 -x 0 #{@sampling/2} -X 'Frequency [Hz]'")
52
+ mag, phase, frq = proc4plot
53
+ GSL::graph(frq, mag, opts)
54
+ end
55
+
56
+ # Display Datasets before and aftern Filtering
57
+ def plotData(opts="-C -g 3")
58
+ GSL::graph(@x,@y, opts + " -L 'Original Data'")
59
+ GSL::graph(@x,@fft.inverse, opts + " -L 'After Filtering'")
60
+ end
61
+
62
+ # little helper method for plotting
63
+ def proc4plot
64
+ y = @fft.subvector(1, @y.size-2).to_complex2
65
+ mag = y.abs
66
+ phase = y.arg
67
+ frq = GSL::Vector.linspace(0, @sampling/2, mag.size)
68
+ [mag,phase,frq]
69
+ end
70
+
71
+ private :proc4plot
72
+ end
73
+
data/rakefile ADDED
@@ -0,0 +1,96 @@
1
+ begin
2
+ require 'rubygems'
3
+ require 'rake/gempackagetask'
4
+ rescue Exception
5
+ nil
6
+ end
7
+ require 'rake/clean'
8
+ require 'rake/testtask'
9
+ require 'rake/rdoctask'
10
+
11
+ SPEC = eval(File.open("gemspec","r").read)
12
+
13
+ def filename_to_sym(filename)
14
+ File.basename(filename,File.extname(filename)).to_sym
15
+ end
16
+
17
+ # ====================================================================
18
+ # TEST TASKS
19
+ test_tasks = {
20
+ :test_all => ["Run all tests"],
21
+ }
22
+ # Syntax checkning task
23
+ task :test_syn do
24
+ Dir.glob("**/*.rb").each {|file|
25
+ printf "Checking Syntax of #{file} ..."
26
+ system("ruby -c #{file}")
27
+ }
28
+ end
29
+ # Test tasks for each test file
30
+ SPEC.test_files.each do |test_file|
31
+ next unless File.extname(test_file) == ".rb"
32
+ Rake::TestTask.new(filename_to_sym(test_file)) do |t|
33
+ test_tasks[:test_all] << filename_to_sym(test_file)
34
+ t.test_files = FileList[test_file]
35
+ t.warning = false
36
+ t.verbose = true
37
+ end
38
+ end
39
+
40
+ # Test Tasks for groups of test files
41
+ test_tasks.each do |k,v|
42
+ desc v[0]
43
+ task k => v[1..-1]
44
+ end
45
+ # ====================================================================
46
+ # Create a task that will package the software into distributable
47
+ # tar, zip and gem files.
48
+ if ! defined?(Gem)
49
+ puts "Package Target requires RubyGEMs"
50
+ else
51
+ package_task = Rake::GemPackageTask.new(SPEC) do |pkg|
52
+ pkg.need_zip = false
53
+ pkg.need_tar = false
54
+ end
55
+ end
56
+ # ====================================================================
57
+ desc "Install the Library with docs"
58
+ task :install => [:repackage] do
59
+ command = "gem install pkg/#{SPEC.name}-#{SPEC.version}.gem"
60
+ puts command
61
+ system(command)
62
+ end
63
+ task :smallInstall => [:repackage] do
64
+ command = "gem install pkg/#{SPEC.name}-#{SPEC.version}.gem --no-ri --no-rdoc"
65
+ puts command
66
+ system(command)
67
+ end
68
+ # ====================================================================
69
+ # Create a task to build the RDOC documentation tree.
70
+ Rake::RDocTask.new("rdoc") { |rdoc|
71
+ rdoc.rdoc_dir = 'rdoc'
72
+ rdoc.title = "Spectral Filtering of datasets through fft-functionality of rb-gsl"
73
+ rdoc.options << '-ad' << '--line-numbers' << '--inline-source'
74
+ rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
75
+ }
76
+ ############################################################
77
+ files = Dir.glob("{lib,test}/*.rb")
78
+ visual_mode = (ENV["vimode"].nil?) ? '-p' : ENV["vimode"]
79
+ desc "Edit texfiles #{files.join(", ")}"
80
+ task :edit do
81
+ com = (File.exist?("Session.vim"))\
82
+ ? 'vim -S'\
83
+ : "vim #{files.join(" ")} rakefile gemspec #{visual_mode}"
84
+ puts com
85
+ system(com)
86
+ end
87
+ ############################################################
88
+ desc "renew the tags file"
89
+ task :tags do
90
+ com = "rtags --vi -f tags lib/*.rb"
91
+ system(com)
92
+ end
93
+ #
94
+ #
95
+ # vim:ft=ruby
96
+ #
@@ -0,0 +1,35 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+ require 'test/unit'
3
+ require 'spectralfilter'
4
+
5
+ class TestSpectralFilter < Test::Unit::TestCase
6
+ N = 2048
7
+ SAMPLING = 2000 # 2 kHz
8
+ TMAX = 1.0/SAMPLING*N
9
+ FREQ1 = 50
10
+ FREQ2 = 120
11
+ FREQ3 = 500
12
+ FREQ4 = 550
13
+ def test_simple
14
+ t = GSL::Vector.linspace(0, TMAX, N)
15
+ x = GSL::Sf::sin(2*Math::PI*FREQ1*t) + \
16
+ GSL::Sf::sin(2*Math::PI*FREQ2*t) + \
17
+ GSL::Sf::sin(2*Math::PI*FREQ3*t) + \
18
+ GSL::Sf::sin(2*Math::PI*FREQ4*t)
19
+
20
+ sf = SpectralFilter.new(t,x)
21
+ opts ="-C -g 3 -x 0 700 "
22
+ sf.plotSpec(opts + "-L'Original Data'")
23
+ sf.lowpass(60)
24
+ sf.plotSpec(opts + "-L 'lowpass(60)'")
25
+ sf.renew
26
+ sf.highpass(450)
27
+ sf.plotSpec(opts + "-L 'highpass(450)'")
28
+ sf.renew
29
+ sf.bandpass(100,520)
30
+ sf.plotSpec(opts + "-L 'bandpass(100,520)'")
31
+ sf.renew
32
+ sf.bandblock(100,520)
33
+ sf.plotSpec(opts + "-L 'bandblock(100,520)'")
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spectralfilter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "Ralf M\xFCller"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-24 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: stark.dreamdetective@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/spectralfilter.rb
26
+ - rakefile
27
+ - gemspec
28
+ - LICENSE
29
+ has_rdoc: true
30
+ homepage: http://rubyforge.org/projects/extcsv
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: Filtering Datasets via Highpass, Lowpass, Bandpass or Bandblock
55
+ test_files:
56
+ - test/test_spectralfilter.rb