-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (71 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* > Cyclic rotation for phase-zero windowing
*
* [](https://npmjs.org/package/dsp-fftshift/)
*
* @example
* var shift = require('dsp-fftshift')
* shift.fftshift(signal)
* shift.ifftshift(signal)
*
* @example
* // ES6 syntax
* import { fftshift, ifftshift } from 'dsp-fftshift'
* fftshift(signal)
*
* @module fftshift
*/
/**
* Rotate a buffer in place
*
* from: http://stackoverflow.com/questions/876293/fastest-algorithm-for-circle-shift-n-sized-array-for-m-position
*
* @param {Array} source - the buffer to rotate
* @param {Number} rotations - the number of rotations
* @private
*/
function rotate (src, n) {
var len = src.length
reverse(src, 0, len)
reverse(src, 0, n)
reverse(src, n, len)
return src
}
function reverse (src, from, to) {
--from
while (++from < --to) {
var tmp = src[from]
src[from] = src[to]
src[to] = tmp
}
}
/**
* Zero-phase windowing alignment
*
* __CAUTION__: this function mutates the array
*
* Perform a cyclic shifting (rotation) to set the first sample at the middle
* of the buffer (it reorder buffer samples from (0:N-1) to [(N/2:N-1) (0:(N/2-1))])
*
* Named by the same function in mathlab: `fftshift`
*
* @param {Array} buffer
* @return {Array} the same buffer (with the data rotated)
*/
export function fftshift (src) {
const len = src.length
return rotate(src, Math.floor(len / 2))
}
/**
* Inverse of zero-phase windowing alignment
*
* __CAUTION__: this function mutates the array
*
* @see fftshift
* @param {Array} buffer
* @return {Array} the same buffer (with the data rotated)
*/
export function ifftshift (src) {
const len = src.length
return rotate(src, Math.floor((len + 1) / 2))
}