mfilter 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1e7491f56a38d509d83bdf57aa7dd2a04f63db5c51c29563d1a9e19a2820b16
4
- data.tar.gz: a3cce8b1e0eb5513cdf22f59d09ab115218fb2254eb9a46a0532673b137e3cce
3
+ metadata.gz: 895c1727433c18838274084fc60e4df657a2b57165390bbe8241cb38b07d4cf2
4
+ data.tar.gz: d3cb564a5a9e0a4a88137fb813023519ab8843d9101c401c1e5f83dabe31939b
5
5
  SHA512:
6
- metadata.gz: 2952d39fe2c612faca418d07bd01bb4042787c8898ce7933d3ca77eb5ccfd4ee18a25433788095e623bc0ca7c3cb44fcb4e3b5a8e520aa6f5a9ae7f02d4f8102
7
- data.tar.gz: 8baf7ca30f4223ca67e5a1c8b73fa0b74058e0dfdf4fa1e6feeef757495519078009281214628bfbc108ff4eae67a143150ad10d5f357d812a45d9d07ff49968
6
+ metadata.gz: e59d5f377934bb688b810b19a2af2db133b68c904cf5b14ffaf9125f99bdfaa39e0345c04d4b08b22f030ac5037fd9f17222e624665cfbdd128bc9aeb517cea9
7
+ data.tar.gz: e0bd2a28a725542c8b527b9ab5fbfc185221edc74c0ffb50b1e64a5b439e98f4764a0f97236cdb6f1780a10ed4190433c27c94da25cc765813ac6c9908d528c2
data/.gitignore CHANGED
@@ -6,4 +6,5 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- /.vscode/
9
+ /.vscode/
10
+ *.dat
data/Gemfile.lock CHANGED
@@ -1,11 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mfilter (0.1.0)
4
+ mfilter (0.2.1)
5
+ numo-narray (~> 0.9.2.0)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
10
+ numo-narray (0.9.2.0)
9
11
  rake (13.0.6)
10
12
  rake-compiler (1.1.1)
11
13
  rake
@@ -19,4 +21,4 @@ DEPENDENCIES
19
21
  rake-compiler (~> 1.1.1)
20
22
 
21
23
  BUNDLED WITH
22
- 2.2.1
24
+ 2.2.22
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # Mfilter
2
+ [![Gem Version](https://badge.fury.io/rb/mfilter.svg)](https://badge.fury.io/rb/mfilter)
3
+
2
4
  MatLab (Octave) ライクの `filter` 関数の Ruby 実装。
3
5
 
4
6
  ## インストール方法
@@ -20,12 +22,28 @@ Gem のインストールを実行:
20
22
  $ gem install specific_install
21
23
  $ gem specific_install -l "himeyama/mfilter.git"
22
24
 
23
- ## 使用法
24
-
25
+ ## 使用例
25
26
  ```ruby
27
+ #!/usr/bin/env ruby
28
+
26
29
  require "mfilter"
30
+
31
+ t = Array.new(100){|i| -Math::PI + i * 2 * Math::PI / (100 - 1)}
32
+ x = t.map{|e| Math::sin(e) + 0.25 * rand}
33
+ b = [0.2] * 5
34
+ a = [1]
35
+
36
+ y = MFilter::filter(b, a, x)
37
+
38
+ open("test/data.dat", "w") do |f|
39
+ f.puts [t, x, y].transpose.map{|e| e.join(" ")}
40
+ end
27
41
  ```
28
42
 
43
+ ![example](https://user-images.githubusercontent.com/39254183/130913789-0245fa7f-1537-456c-8669-58ef9d9ab89c.png)
44
+
45
+
46
+
29
47
  ## Contributing
30
48
 
31
49
  Bug reports and pull requests are welcome on GitHub at https://github.com/himeyama/mfilter.
data/Rakefile CHANGED
@@ -6,4 +6,13 @@ task default: %i[]
6
6
  require "rake/extensiontask"
7
7
  Rake::ExtensionTask.new "mfilter" do |ext|
8
8
  ext.lib_dir = "ext"
9
+ end
10
+
11
+
12
+ require "rake/testtask"
13
+ Rake::TestTask.new :test do |t|
14
+ t.libs << "test"
15
+ t.libs << "lib"
16
+ t.verbose = true
17
+ t.test_files = FileList["test/*.rb"]
9
18
  end
@@ -1,7 +1,13 @@
1
1
  require "mkmf"
2
2
 
3
- have_library("octave")
3
+ oc_file = "/usr/include/octave-#{`octave-config -p VERSION`.chop}"
4
+ $INCFLAGS += " -I#{oc_file}" if File.exist? oc_file
5
+ have_library("octave")
4
6
  have_library("octinterp")
5
7
  have_library("m")
6
8
 
9
+ narray_dir = Gem.find_files("numo").map{|e| e.include?("narray") ? e : false}.select{|e| e}.to_a[0]
10
+ dir_config('narray', narray_dir, narray_dir)
11
+ have_library("narray")
12
+
7
13
  create_makefile "mfilter"
data/ext/mfilter/main.cc CHANGED
@@ -1,32 +1,88 @@
1
- #include <octave-5.2.0/octave/oct.h>
1
+ #include <octave/oct.h>
2
2
  #include <iostream>
3
3
  #include <math.h>
4
4
  #include <vector>
5
5
  #include "filter.hpp"
6
6
  #include <ruby.h>
7
+ #include <numo/narray.h>
7
8
 
9
+ extern VALUE numo_cDFloat;
8
10
 
9
- VALUE rb_m_filter(VALUE self, VALUE b, VALUE a, VALUE x){
11
+ double* NA_GET_DBL_PT(VALUE na){
12
+ double* p = (double*)na_get_pointer(na);
13
+ long offset = na_get_offset(na) / sizeof(double);
14
+ return p + offset;
15
+ }
16
+
17
+ VALUE rb_na_filter(VALUE self, VALUE b, VALUE a, VALUE x, VALUE si){
18
+ size_t len[3] = {RNARRAY_SIZE(b), RNARRAY_SIZE(a), RNARRAY_SIZE(x)};
19
+ MArray<double>
20
+ mb(dim_vector(len[0], 1)),
21
+ ma(dim_vector(len[1], 1)),
22
+ mx(dim_vector(len[2], 1)),
23
+ my;
24
+ memcpy((double*)mb.data(), NA_GET_DBL_PT(b), sizeof(double) * len[0]);
25
+ memcpy((double*)ma.data(), NA_GET_DBL_PT(a), sizeof(double) * len[1]);
26
+ memcpy((double*)mx.data(), NA_GET_DBL_PT(x), sizeof(double) * len[2]);
27
+ VALUE y = rb_funcall(numo_cDFloat, rb_intern("zeros"), 1, LONG2NUM(len[2]));
28
+
29
+ if(si == Qnil)
30
+ my = filter(mb, ma, mx);
31
+ else if(rb_obj_class(si) == numo_cDFloat){
32
+ long lsi = NUM2LONG(rb_funcall(si, rb_intern("size"), 0));
33
+ MArray<double> msi(dim_vector(lsi, 1));
34
+ double* namsi = NA_GET_DBL_PT(si);
35
+ memcpy((double*)msi.data(), namsi, sizeof(double) * lsi);
36
+ my = filter(mb, ma, mx, msi);
37
+ }
38
+ else
39
+ rb_raise(rb_eTypeError, "si should belong to Numo::DFloat class");
40
+ memcpy(NA_GET_DBL_PT(y), (double*)my.data(), sizeof(double) * len[2]);
41
+
42
+ return y;
43
+ }
44
+
45
+
46
+ VALUE rb_m_filter(VALUE self, VALUE b, VALUE a, VALUE x, VALUE si){
10
47
  MArray<double>
11
48
  mb(dim_vector(RARRAY_LEN(b), 1)),
12
49
  ma(dim_vector(RARRAY_LEN(a), 1)),
13
- mx(dim_vector(RARRAY_LEN(x), 1));
14
- for(long i = 0; i < RARRAY_LEN(b); i++)
15
- mb(i, 0) = NUM2DBL(rb_ary_entry(b, i));
16
- for(long i = 0; i < RARRAY_LEN(a); i++)
17
- ma(i, 0) = NUM2DBL(rb_ary_entry(a, i));
18
- for(long i = 0; i < RARRAY_LEN(x); i++)
19
- mx(i, 0) = NUM2DBL(rb_ary_entry(x, i));
20
- MArray<double> my = filter(mb, ma, mx);
21
- VALUE y = rb_ary_new();
50
+ mx(dim_vector(RARRAY_LEN(x), 1)),
51
+ my;
52
+ double* p[3] = {(double*)mb.data(), (double*)ma.data(), (double*)mx.data()};
53
+ const VALUE* pr[3] = {
54
+ RARRAY_CONST_PTR_TRANSIENT(b),
55
+ RARRAY_CONST_PTR_TRANSIENT(a),
56
+ RARRAY_CONST_PTR_TRANSIENT(x)
57
+ };
58
+ long len[3] = {RARRAY_LEN(b), RARRAY_LEN(a), RARRAY_LEN(x)};
59
+ VALUE y;
60
+ for(int n = 0; n < 3; n++)
61
+ for(long i = 0; i < len[n]; i++)
62
+ p[n][i] = NUM2DBL(pr[n][i]);
63
+
64
+ if(si == Qnil)
65
+ my = filter(mb, ma, mx);
66
+ else if(rb_obj_class(si) == rb_cArray){
67
+ MArray<double> msi(dim_vector(RARRAY_LEN(si), 1));
68
+ for(long i = 0; i < RARRAY_LEN(si); i++)
69
+ msi(i, 0) = NUM2DBL(rb_ary_entry(si, i));
70
+ my = filter(mb, ma, mx, msi);
71
+ }
72
+ else
73
+ rb_raise(rb_eTypeError, "si should belong to Array class");
74
+
75
+ y = rb_ary_new();
22
76
  for(long i = 0; i < RARRAY_LEN(x); i++)
23
77
  rb_ary_store(y, i, DBL2NUM(my(i, 0)));
78
+
24
79
  return y;
25
80
  }
26
81
 
27
82
  extern "C"{
28
83
  void Init_mfilter(){
29
84
  VALUE rb_cMFilter = rb_define_module("MFilter");
30
- rb_define_module_function(rb_cMFilter, "filter", RUBY_METHOD_FUNC(rb_m_filter), 3);
85
+ rb_define_module_function(rb_cMFilter, "_filter", rb_m_filter, 4);
86
+ rb_define_module_function(rb_cMFilter, "na_filter", rb_na_filter, 4);
31
87
  }
32
88
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mfilter
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/mfilter.rb CHANGED
@@ -2,9 +2,26 @@
2
2
 
3
3
  require_relative "mfilter/version"
4
4
 
5
+ require "numo/narray"
5
6
  require "mfilter.so"
6
7
 
7
- module Mfilter
8
- class Error < StandardError; end
9
- # Your code goes here...
8
+ module MFilter
9
+ class Error < StandardError; end
10
+
11
+ def filter(b, a, x, si: nil)
12
+ raise TypeError, "b should belong to Array or Numeric class" unless b.is_a? Array or b.is_a? Numeric or b.is_a? Numo::DFloat
13
+ raise TypeError, "a should belong to Array or Numeric class" unless a.is_a? Array or a.is_a? Numeric or a.is_a? Numo::DFloat
14
+ raise TypeError, "x should belong to Array class" unless x.is_a? Array or x.is_a? Numo::DFloat
15
+ raise TypeError, "si should be nil or belong to Array class" unless si.is_a? Array or si.nil? or si.is_a? Numo::DFloat
16
+ b = [b.to_f] if b.is_a? Numeric
17
+ a = [a.to_f] if a.is_a? Numeric
18
+ b, a, x = [Numo::DFloat.cast(b), Numo::DFloat.cast(a), Numo::DFloat.cast(x)]
19
+ si = Numo::DFloat.cast(si) if si
20
+
21
+ if b.is_a?(Numo::DFloat) and a.is_a?(Numo::DFloat) and x.is_a?(Numo::DFloat)
22
+ return na_filter(b, a, x, si)
23
+ end
24
+ end
25
+
26
+ module_function :filter
10
27
  end
data/mfilter.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "rake", ">= 12.3.3"
22
22
  spec.add_development_dependency "rake-compiler", "~> 1.1.1"
23
+ spec.add_dependency "numo-narray", "~> 0.9.2.0"
23
24
  end
data/mkmf.log ADDED
@@ -0,0 +1,37 @@
1
+ have_library: checking for -lnarray... -------------------- no
2
+
3
+ "gcc -o conftest -I/home/hikari/.rbenv/versions/3.0.2/include/ruby-3.0.0/x86_64-linux -I/home/hikari/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/backward -I/home/hikari/.rbenv/versions/3.0.2/include/ruby-3.0.0 -I. -I/home/hikari/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/numo-narray-0.9.2.0/lib/numo -I/home/hikari/.rbenv/versions/3.0.2/include -O3 -ggdb3 -Wall -Wextra -Wdeprecated-declarations -Wduplicated-cond -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wwrite-strings -Wimplicit-fallthrough=0 -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-packed-bitfield-compat -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wunused-variable -fPIC conftest.c -L. -L/home/hikari/.rbenv/versions/3.0.2/lib -Wl,-rpath,/home/hikari/.rbenv/versions/3.0.2/lib -L/home/hikari/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/numo-narray-0.9.2.0/lib/numo -Wl,-rpath,/home/hikari/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/numo-narray-0.9.2.0/lib/numo -L. -L/home/hikari/.rbenv/versions/3.0.2/lib -fstack-protector-strong -rdynamic -Wl,-export-dynamic -Wl,-rpath,/home/hikari/.rbenv/versions/3.0.2/lib -L/home/hikari/.rbenv/versions/3.0.2/lib -lruby -lm -lc"
4
+ checked program was:
5
+ /* begin */
6
+ 1: #include "ruby.h"
7
+ 2:
8
+ 3: int main(int argc, char **argv)
9
+ 4: {
10
+ 5: return !!argv[argc];
11
+ 6: }
12
+ /* end */
13
+
14
+ "gcc -o conftest -I/home/hikari/.rbenv/versions/3.0.2/include/ruby-3.0.0/x86_64-linux -I/home/hikari/.rbenv/versions/3.0.2/include/ruby-3.0.0/ruby/backward -I/home/hikari/.rbenv/versions/3.0.2/include/ruby-3.0.0 -I. -I/home/hikari/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/numo-narray-0.9.2.0/lib/numo -I/home/hikari/.rbenv/versions/3.0.2/include -O3 -ggdb3 -Wall -Wextra -Wdeprecated-declarations -Wduplicated-cond -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wwrite-strings -Wimplicit-fallthrough=0 -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-packed-bitfield-compat -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wunused-variable -fPIC conftest.c -L. -L/home/hikari/.rbenv/versions/3.0.2/lib -Wl,-rpath,/home/hikari/.rbenv/versions/3.0.2/lib -L/home/hikari/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/numo-narray-0.9.2.0/lib/numo -Wl,-rpath,/home/hikari/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/numo-narray-0.9.2.0/lib/numo -L. -L/home/hikari/.rbenv/versions/3.0.2/lib -fstack-protector-strong -rdynamic -Wl,-export-dynamic -Wl,-rpath,/home/hikari/.rbenv/versions/3.0.2/lib -L/home/hikari/.rbenv/versions/3.0.2/lib -lruby -lnarray -lm -lc"
15
+ /home/hikari/.linuxbrew/bin/ld: cannot find -lnarray
16
+ collect2: error: ld returned 1 exit status
17
+ checked program was:
18
+ /* begin */
19
+ 1: #include "ruby.h"
20
+ 2:
21
+ 3: /*top*/
22
+ 4: extern int t(void);
23
+ 5: int main(int argc, char **argv)
24
+ 6: {
25
+ 7: if (argc > 1000000) {
26
+ 8: int (* volatile tp)(void)=(int (*)(void))&t;
27
+ 9: printf("%d", (*tp)());
28
+ 10: }
29
+ 11:
30
+ 12: return !!argv[argc];
31
+ 13: }
32
+ 14:
33
+ 15: int t(void) { ; return 0; }
34
+ /* end */
35
+
36
+ --------------------
37
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mfilter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Murata Mitsuharu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-26 00:00:00.000000000 Z
11
+ date: 2021-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.1.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: numo-narray
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.2.0
41
55
  description:
42
56
  email:
43
57
  - hikari.photon+dev@gmail.com
@@ -60,6 +74,7 @@ files:
60
74
  - lib/mfilter.rb
61
75
  - lib/mfilter/version.rb
62
76
  - mfilter.gemspec
77
+ - mkmf.log
63
78
  homepage:
64
79
  licenses: []
65
80
  metadata: {}