cowboy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cowboy.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alejandro Ciniglio
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Cowboy
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cowboy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cowboy
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/cowboy.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/cowboy/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Alejandro Ciniglio"]
6
+ gem.email = ["mail@alejandrociniglio.com"]
7
+ gem.description = %q{gem description}
8
+ gem.summary = %q{gem summary}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "cowboy"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Cowboy::VERSION
17
+
18
+ gem.extensions = ["ext/cowboy/extconf.rb"]
19
+ end
@@ -0,0 +1,35 @@
1
+ #include "cowboy.h"
2
+
3
+ VALUE mCowboy;
4
+
5
+ VALUE fft_1d(VALUE m, VALUE nums){
6
+ fftw_complex *in, *out;
7
+ fftw_plan fp;
8
+ long n;
9
+
10
+ Check_Type(nums, T_ARRAY);
11
+
12
+ n = size_of_ary(nums);
13
+ if (n == 0){
14
+ rb_raise(rb_eException, "Can't use blank array");
15
+ }
16
+ in = allocate_fftw_complex(n);
17
+ out = allocate_fftw_complex(n);
18
+ fp = fftw_plan_dft_1d(n, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
19
+
20
+ cast_nums_to_complex(in, nums);
21
+
22
+ fftw_execute(fp);
23
+ free(in);
24
+ fftw_destroy_plan(fp);
25
+
26
+ return complex_to_real_nums(out, n);
27
+ }
28
+
29
+ void Init_cowboy(){
30
+ mCowboy = rb_define_module("Cowboy");
31
+
32
+ Init_cowboy_complex();
33
+
34
+ rb_define_module_function(mCowboy, "fft_1d", fft_1d, 1);
35
+ }
@@ -0,0 +1,11 @@
1
+ #ifndef RUBY_COWBOY
2
+ #define RUBY_COWBOY
3
+
4
+ #include <ruby.h>;
5
+ #include <fftw3.h>;
6
+
7
+ #include "cowboy_complex.h";
8
+
9
+ extern VALUE mCowboy;
10
+
11
+ #endif
@@ -0,0 +1,56 @@
1
+ #include "cowboy.h"
2
+ #include <ruby.h>
3
+
4
+ long size_of_ary(VALUE nums){
5
+ Check_Type(nums, T_ARRAY);
6
+ return RARRAY_LEN(nums);
7
+ }
8
+
9
+ void cast_nums_to_complex(fftw_complex * fc, VALUE nums){
10
+ long len;
11
+ int i;
12
+ VALUE n;
13
+ Check_Type(nums, T_ARRAY);
14
+ i = 0;
15
+
16
+ while((n =rb_ary_shift(nums)) != Qnil){
17
+ fc[i][0] = NUM2DBL(n);
18
+ fc[i][1] = 0;
19
+ i++;
20
+ }
21
+ }
22
+
23
+ fftw_complex * allocate_fftw_complex(long n){
24
+ fftw_complex * fc;
25
+ fc = ALLOC_N(fftw_complex, n);
26
+ return fc;
27
+ }
28
+
29
+ VALUE c_to_rb_complex(double r, double i){
30
+ VALUE cplx = rb_path2class("Complex");
31
+ return rb_funcall(cplx, rb_intern("rect"), 2,
32
+ DBL2NUM(r), DBL2NUM(i));
33
+ }
34
+
35
+ VALUE complex_to_real_nums(fftw_complex *fc, long N){
36
+ VALUE ar = rb_ary_new();
37
+ int i;
38
+ for(i = 0; i < N; i++){
39
+ rb_ary_push(ar, c_to_rb_complex(fc[i][0],
40
+ fc[i][1]));
41
+ }
42
+ return ar;
43
+ }
44
+
45
+ VALUE test_back_and_from_complex(VALUE m, VALUE nums){
46
+ Check_Type(nums, T_ARRAY);
47
+ fftw_complex * fc;
48
+ fc = allocate_fftw_complex(size_of_ary(nums));
49
+ cast_nums_to_complex(fc, nums);
50
+ return complex_to_real_nums(fc,
51
+ size_of_ary(nums));
52
+ }
53
+
54
+ void Init_cowboy_complex(){
55
+ rb_define_module_function(mCowboy, "test_complex", test_back_and_from_complex, 1);
56
+ }
@@ -0,0 +1,14 @@
1
+ #ifndef RUBY_COWBOY_COMPLEX
2
+ #define RUBY_COWBOY_COMPLEX
3
+
4
+ #include <cowboy.h>
5
+
6
+ void Init_cowboy_complex();
7
+
8
+ void cast_nums_to_complex(fftw_complex * fc, VALUE nums);
9
+ fftw_complex * allocate_fftw_complex(long n);
10
+ VALUE complex_to_real_nums(fftw_complex * fc, long N);
11
+ long size_of_ary(VALUE nums);
12
+
13
+
14
+ #endif
@@ -0,0 +1,29 @@
1
+ require 'mkmf'
2
+ LIBDIR = RbConfig::CONFIG['libdir']
3
+ INCLUDEDIR = RbConfig::CONFIG['includedir']
4
+
5
+ HEADER_DIRS = [
6
+ '/opt/local/include',
7
+ '/usr/local/include',
8
+ INCLUDEDIR,
9
+ '/usr/include',
10
+ ]
11
+
12
+ LIB_DIRS = [
13
+ '/opt/local/lib',
14
+ '/usr/local/lib',
15
+ LIBDIR,
16
+ '/usr/lib',
17
+ ]
18
+
19
+ dir_config('fftw', HEADER_DIRS, LIB_DIRS)
20
+
21
+ unless find_header('fftw3.h')
22
+ abort "FFTw is missing, please install FFTw"
23
+ end
24
+
25
+ unless find_library('fftw3', "fftw_malloc")
26
+ abort "FFTw is missing, please install FFTw"
27
+ end
28
+
29
+ create_makefile('cowboy/cowboy')
data/lib/cowboy.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "cowboy/version"
2
+
3
+ require 'cowboy/cowboy'
4
+ module Cowboy
5
+ # Your code goes here...
6
+ end
7
+
8
+ require "cowboy/hamming"
9
+ require "cowboy/fft"
data/lib/cowboy/fft.rb ADDED
@@ -0,0 +1,39 @@
1
+ module Cowboy
2
+ class Frequencies
3
+ attr_reader :dc, :unshifted, :freq
4
+
5
+ def initialize(unshifted_array)
6
+ @unshifted = unshifted_array
7
+ @dc = unshifted_array[0].real
8
+ n = unshifted_array.size
9
+ @freq = []
10
+ for i in (n/2...n)
11
+ @freq << unshifted_array[i].abs
12
+ end
13
+ for i in (0...n/2)
14
+ @freq << unshifted_array[i].abs
15
+ end
16
+ end
17
+
18
+ def buckets(sample_rate=nil)
19
+ sample_rate ||= @freq.size
20
+ nyquist = sample_rate/2.0
21
+ a = []
22
+ n = @freq.size
23
+ for i in (0...n)
24
+ a << -nyquist + (2 * nyquist/n) * i
25
+ end
26
+ return a
27
+ end
28
+ end
29
+
30
+ def Cowboy::fft(arr, w=Hamming, n=29)
31
+ if !w.nil?
32
+ window = w.new(n)
33
+ input = window.convolve(arr)
34
+ fft_1d(input)
35
+ else
36
+ fft_1d(arr)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ class Cowboy::Hamming
2
+ def initialize(n)
3
+ @n = n
4
+ create_kernel
5
+ end
6
+
7
+ def convolve(arr)
8
+ raise "Array smaller than window size" if arr.size < @n
9
+ a = []
10
+ for i in (0...(arr.size - (@n - 1)))
11
+ n = 0.0
12
+ for j in (0...@n)
13
+ c = @kernel[j] * arr[i+j]
14
+ n += c
15
+ end
16
+ a << n
17
+ end
18
+ return a
19
+ end
20
+
21
+ private
22
+ def create_kernel
23
+ a = []
24
+ for j in (0...@n)
25
+ c = 0.54 - 0.46 * Math.cos((2*Math::PI * j)/(@n - 1))
26
+ a << c
27
+ end
28
+ @kernel = a
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Cowboy
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cowboy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alejandro Ciniglio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: gem description
15
+ email:
16
+ - mail@alejandrociniglio.com
17
+ executables: []
18
+ extensions:
19
+ - ext/cowboy/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - cowboy.gemspec
28
+ - ext/cowboy/cowboy.c
29
+ - ext/cowboy/cowboy.h
30
+ - ext/cowboy/cowboy_complex.c
31
+ - ext/cowboy/cowboy_complex.h
32
+ - ext/cowboy/extconf.rb
33
+ - lib/cowboy.rb
34
+ - lib/cowboy/fft.rb
35
+ - lib/cowboy/hamming.rb
36
+ - lib/cowboy/version.rb
37
+ homepage: ''
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 1.8.24
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: gem summary
61
+ test_files: []