mfcc 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13ccb341c81931d49deda928b632b66f1c3530ea
4
+ data.tar.gz: 27c2b78a7c3a4d7927de7e50e4771fad21710bbf
5
+ SHA512:
6
+ metadata.gz: 917228307517bd92ef1228b8fc52346e783f29e019f5d801436ef8b47c049092ad218b869e9c0102e33ecf593cb994c1ebf71ca1c4742ead1abbb054c72bdbc6
7
+ data.tar.gz: f462979ee7b29a2f3c434f33550ecd75f1480694243f95c2afdf26179e4ce1b1368a75b4e9cbc904a0c2d8f80a92caeec827919b6b8d1a6d5d6f809d4690905e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,6 @@
1
+ inherit_from:
2
+ - .rubocop_todo.yml
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - "*.gemspec"
@@ -0,0 +1,15 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-05-25 08:46:53 -0700 using RuboCop version 0.31.0.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 6
9
+ # Configuration parameters: AllowURI, URISchemes.
10
+ Metrics/LineLength:
11
+ Max: 156
12
+
13
+ # Offense count: 2
14
+ Style/Documentation:
15
+ Enabled: false
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mfcc.gemspec
4
+ gemspec
@@ -0,0 +1,39 @@
1
+ # Mfcc
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mfcc`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'mfcc'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install mfcc
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/mfcc/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mfcc'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,61 @@
1
+ require 'mfcc/version'
2
+
3
+ # MFCCs are commonly derived as follows:
4
+ #
5
+ # - Take the Fourier transform of (a windowed excerpt of) a signal.
6
+ # - Map the powers of the spectrum obtained above onto the mel scale, using triangular overlapping windows.
7
+ # - Take the logs of the powers at each of the mel frequencies.
8
+ # - Take the discrete cosine transform of the list of mel log powers, as if it were a signal.
9
+ # - The MFCCs are the amplitudes of the resulting spectrum.
10
+ # (http://en.wikipedia.org/wiki/Mel-frequency_cepstrum)
11
+ module Mfcc
12
+ class Calculator
13
+ require 'mfcc/preemphasis'
14
+ require 'mfcc/frame'
15
+ require 'mfcc/hamming'
16
+ require 'mfcc/dft'
17
+ require 'mfcc/mel'
18
+ require 'mfcc/compressor'
19
+ require 'mfcc/dct'
20
+
21
+ include Mfcc::Preemphasis
22
+ include Mfcc::Frame
23
+ include Mfcc::Hamming
24
+ include Mfcc::Dft
25
+ include Mfcc::Mel
26
+ include Mfcc::Compressor
27
+ include Mfcc::Dct
28
+
29
+ attr_reader :data, :options, :frame_size, :frame_step, :alpha, :emphasis,
30
+ :mel_filters_length, :fft_size, :sample_rate, :dct_order
31
+
32
+ def initialize(data, options = {})
33
+ @data = data
34
+ @frame_size = options.fetch(:frame_size, 400)
35
+ @frame_step = options.fetch(:frame_step, 160)
36
+ @alpha = options.fetch(:alpha, 0.46)
37
+ @emphasis = options.fetch(:emphasis, 0.97)
38
+ @mel_filters_length = options.fetch(:mel_filters_length, 20)
39
+ @fft_size = options.fetch(:fft_size, 512)
40
+ @sample_rate = options.fetch(:sample_rate, 16_000)
41
+ @dct_order = options.fetch(:dct_order, 13)
42
+ end
43
+
44
+ def map
45
+ return to_enum(:map) { self.data.size || Float::Infinity } unless block_given?
46
+
47
+ data = preemphasis(self.data)
48
+
49
+ data = frame(data)
50
+
51
+ data = data.map do |frame|
52
+ frame = hamming(frame)
53
+ frame = dft(frame)
54
+ frame = Mfcc.magnitude(frame)
55
+ frame = compress(frame)
56
+ frame = dct(frame)
57
+ yield frame
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,23 @@
1
+ module Mfcc
2
+ module Compressor
3
+ def compress(data)
4
+ Mfcc.compress(data)
5
+ end
6
+ end
7
+
8
+ LOG10_ERROR_VALUE = -0.00000001
9
+
10
+ def self.compress(data)
11
+ return to_enum(:compress, data) { data.size } unless block_given?
12
+
13
+ data.each do |d|
14
+ v = if d == 0
15
+ LOG10_ERROR_VALUE
16
+ else
17
+ Math.log10(d)
18
+ end
19
+
20
+ yield v
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ module Mfcc
2
+ # Takes the discrete cosing transform. Converts an n x m matrix to an n x
3
+ # order matrix. ncol should be set to m.
4
+ module Dct
5
+ def dct(data)
6
+ Mfcc.dct(data, dct_order)
7
+ end
8
+ end
9
+
10
+ def self.dct(data, order = 13, orthogonalize = true)
11
+ length = data.size
12
+
13
+ scales = if orthogonalize
14
+ [Math.sqrt(1.0 / (4 * length)), Math.sqrt(1.0 / (2 * length))]
15
+ else
16
+ [1, 1]
17
+ end
18
+
19
+ dct_matrix(order, length).each_with_index.map do |row, index|
20
+ scale = index == 0 ? scales[0] : scales[1]
21
+ scale * row.zip(data).inject(0) { |memo, (a, b)| memo + (2 * a * b) }
22
+ end
23
+ end
24
+
25
+ def self.dct_matrix(n, m)
26
+ n.times.map do |i|
27
+ freq = (Math::PI * i) / m
28
+ m.times.map do |j|
29
+ Math.cos(freq * (j + 0.5))
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,52 @@
1
+ require 'complex'
2
+
3
+ module Mfcc
4
+ module Dft
5
+ def dft(data)
6
+ Mfcc.dft(data)
7
+ end
8
+ end
9
+
10
+ def self.dft(data)
11
+ return to_enum(:dft, data) { data.size } unless block_given?
12
+
13
+ n = data.size
14
+
15
+ data = plus_imaginary(data)
16
+
17
+ data.each_with_index do |_, k|
18
+ sumreal = 0
19
+ sumimag = 0
20
+
21
+ data.each_with_index do |d, t|
22
+ angle = 2 * Math::PI * t * k / n
23
+
24
+ sumreal += d.real * Math.cos(angle) + d.imaginary * Math.sin(angle)
25
+ sumimag += -1 * d.real * Math.sin(angle) + d.imaginary * Math.cos(angle)
26
+ end
27
+
28
+ yield Complex(sumreal, sumimag)
29
+ end
30
+ end
31
+
32
+ def self.plus_imaginary(data)
33
+ return to_enum(:plus_imaginary, data) { data.size } unless block_given?
34
+
35
+ data.each do |d|
36
+ yield case d
37
+ when Complex
38
+ d
39
+ else
40
+ Complex(d, 0)
41
+ end
42
+ end
43
+ end
44
+
45
+ def self.magnitude(data)
46
+ return to_enum(:magnitude, data) { data.size } unless block_given?
47
+
48
+ data.each do |d|
49
+ yield d.respond_to?(:magnitude) ? d.magnitude : d
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,33 @@
1
+ module Mfcc
2
+ module Frame
3
+ def frame(data)
4
+ Mfcc.frame(data, frame_size, frame_step)
5
+ end
6
+ end
7
+
8
+ def self.frame(data, size, step)
9
+ return to_enum(:frame, data, size, step) unless block_given?
10
+
11
+ buffer = []
12
+
13
+ data.each do |d|
14
+ buffer.push(d)
15
+
16
+ if buffer.size == size
17
+ yield buffer
18
+
19
+ buffer = buffer.slice(step..buffer.length)
20
+ end
21
+ end
22
+
23
+ length = buffer.length
24
+
25
+ (length / step.to_f).ceil.times do
26
+ buffer.fill(0, buffer.size...size)
27
+
28
+ yield buffer
29
+
30
+ buffer = buffer.slice(step..buffer.length)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ module Mfcc
2
+ module Hamming
3
+ def hamming(data)
4
+ Mfcc.hamming(data, alpha)
5
+ end
6
+ end
7
+
8
+ def self.hamming(data, alpha = 0.46)
9
+ return to_enum(:hamming, data, alpha) { data.size } unless block_given?
10
+
11
+ beta = 1 - alpha
12
+ length = data.size
13
+
14
+ data.each_with_index do |d, i|
15
+ yield d * h(alpha, beta, length, i)
16
+ end
17
+ end
18
+
19
+ def self.h(alpha, beta, size, i)
20
+ alpha - beta * Math.cos(2 * Math::PI * i / (size - 1))
21
+ end
22
+ end
@@ -0,0 +1,60 @@
1
+ module Mfcc
2
+ module Mel
3
+ def mel(data)
4
+ Mfcc.mel(data, filter_banks)
5
+ end
6
+
7
+ def filter_banks
8
+ @filter_banks ||= Mfcc::FilterBanks.new(mel_filters_length, fft_size, sample_rate)
9
+ end
10
+ end
11
+
12
+ def self.mel(data, filter_banks)
13
+ filter_banks.process(data)
14
+ end
15
+
16
+ class FilterBanks
17
+ attr_reader :filters, :fft_size, :samplerate, :range
18
+ def initialize(filters = 20, fft_size = 512, samplerate = 16_000, lowfreq = 0, highfreq = nil)
19
+ @filters = filters
20
+ @fft_size = fft_size
21
+ @samplerate = samplerate
22
+ @range = lowfreq..(highfreq || (samplerate / 2))
23
+ end
24
+
25
+ def process(fft)
26
+ (0..filters).map { |n| fft.zip(bank[n]).reject { |(_, b)| b.nil? }.inject(0) { |memo, (a, b)| memo + a * b } }
27
+ end
28
+
29
+ def bank(i)
30
+ lower = fft_frequencies.map { |x| (x - mel_frequencies[i]) / (mel_frequencies[i + 1] - mel_frequencies[i]).to_f }
31
+ upper = fft_frequencies.map { |x| (mel_frequencies[i + 2] - x) / (mel_frequencies[i + 2] - mel_frequencies[i + 1]).to_f }
32
+
33
+ (0..(fft_size / 2)).map { |n| [0, [lower[n], upper[n]].min].max }
34
+ end
35
+
36
+ def r(i)
37
+ mel_frequencies[i]
38
+ end
39
+
40
+ def to_hz(mel)
41
+ 700 * (10**(mel / 2595.0) - 1)
42
+ end
43
+
44
+ def to_mel(hz)
45
+ 2595 * Math.log10(1 + hz / 700.0)
46
+ end
47
+
48
+ def mel_frequencies
49
+ @mel_frequencies ||= range.step(mel_step).to_a
50
+ end
51
+
52
+ def mel_step
53
+ ((range.end - range.begin) / filters)
54
+ end
55
+
56
+ def fft_frequencies
57
+ @fft_frequencies ||= [0] + (fft_size / 2).times.map { |i| (i + 1) * (samplerate / fft_size.to_f) }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,20 @@
1
+ module Mfcc
2
+ module Preemphasis
3
+ def preemphasis(data)
4
+ Mfcc.preemphasis(data, emphasis)
5
+ end
6
+ end
7
+
8
+ def self.preemphasis(data, emph = 0.97)
9
+ return data if emph == 0
10
+
11
+ return to_enum(:preemphasis, data, emph) { |d, _| d.size || Float::INFINITY } unless block_given?
12
+
13
+ prev = 0
14
+
15
+ data.each do |y|
16
+ yield (y - emph * prev)
17
+ prev = y
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Mfcc
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mfcc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mfcc'
8
+ spec.version = Mfcc::VERSION
9
+ spec.authors = ['Chris Beer']
10
+ spec.email = ['chris@cbeer.info']
11
+
12
+ spec.summary = 'Mel-frequency cepstral coefficients for Ruby'
13
+ spec.homepage = 'https://github.com/cbeer/mfcc'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.9'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rspec', '~> 3.0'
23
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mfcc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Beer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - chris@cbeer.info
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".rubocop_todo.yml"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/mfcc.rb
73
+ - lib/mfcc/compressor.rb
74
+ - lib/mfcc/dct.rb
75
+ - lib/mfcc/dft.rb
76
+ - lib/mfcc/frame.rb
77
+ - lib/mfcc/hamming.rb
78
+ - lib/mfcc/mel.rb
79
+ - lib/mfcc/preemphasis.rb
80
+ - lib/mfcc/version.rb
81
+ - mfcc.gemspec
82
+ homepage: https://github.com/cbeer/mfcc
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.5
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Mel-frequency cepstral coefficients for Ruby
105
+ test_files: []