Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

38
LINES

< > BotCompany Repo | #1032977 // SumOfVibrations

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6058L/35K).

1  
// This looks at a number of periods of a given frequency starting at a certain time in the audio
2  
// and returns an intensity value.
3  
// No phase adjustment here, so you have to call this twice to get meaningful (complex) results.
4  
srecord noeq SumOfVibrations(IAudioSample sample, int channel, double start, double freq, int periods) {
5  
  double period, end;
6  
  
7  
  double rawSum() {
8  
    period = sample.sampleRate()/freq;
9  
    double sum = 0, t = start;
10  
    for p to periods: {
11  
      // Subtract an expected trough from an expected neighboring peak and add to overall sum.
12  
      // Nota bene: Trough and peak have the same area (=length), so this is basically a Haar-like feature!
13  
      // By the use of which we automatically get around nasty complications like DC offsets in the input data.
14  
      
15  
      sum += sample.sampleSum(channel, t, t+period/2)
16  
           - sample.sampleSum(channel, t+period/2, t+period);
17  
        
18  
      t += period;
19  
    }
20  
    end = t;
21  
    ret sum;
22  
  }
23  
  
24  
  // alternate calculation adjusted for duration
25  
  double sumDividedByDuration() {
26  
    ret rawSum()/(end-start);
27  
  }
28  
  
29  
  // Not divided by duration - this seems like the best frequency detector at this point.
30  
  // As in a proper FFT/DCT, we return a complex value to represent phase.
31  
  // Call abs() to get the desired intensity value.
32  
  Complex complexSum() {
33  
    SumOfVibrations sum2 = new(sample, channel, start+period/4, freq, periods);
34  
    ret Complex(rawSum(), sum2.rawSum());
35  
  }
36  
  
37  
  double amplitude() { ret abs(complexSum()); }
38  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032977
Snippet name: SumOfVibrations
Eternal ID of this version: #1032977/7
Text MD5: dcc0bac5a491590dc3fc4323ce550c68
Transpilation MD5: e6b52075ae2c28843451ce88770657b5
Author: stefan
Category: javax / audio analysis
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-11 07:05:47
Source code size: 1556 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 102 / 199
Version history: 6 change(s)
Referenced in: [show references]