Skip to content

Commit

Permalink
minify
Browse files Browse the repository at this point in the history
  • Loading branch information
Hideto Manjo committed Aug 6, 2017
1 parent 5ca12ff commit f48e8a7
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 149 deletions.
270 changes: 131 additions & 139 deletions jquery.fft.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,146 +23,138 @@
this.settings = settings || {samplingrate: 1};
};

FFT.prototype.dim = function dim(F1, F2){
var N = F1.length;

//limited 2^x
while(true){
if( !(N & (N-1)) ){
break;
}
N = N + 1;
F1.push(0);
F2.push(0);
}
return N;
};

FFT.prototype._forloop = function _forloop(N, callback){
var m = N / 2;
var ans = [];
for (var i = 0; i < m; i = i + 1){
ans.push( callback.call(null, i) );
}
return ans;
};

FFT.prototype._convertTo = function _convertTo(unit, F1, F2, samplingrate){
var sps = samplingrate || this.settings.samplingrate;
var f;
switch(unit){
case 'amplitude':
f = function amp(i){return Math.sqrt(F1[i] * F1[i] + F2[i] * F2[i]);};
break;
case 'power':
f = function pow(i){return F1[i] * F1[i] + F2[i] * F2[i];};
break;
case 'phase':
f = function phs(i){return Math.atan2(F2[i], F1[i]);};
break;
case 'frequencies':
f = function freq(i){return sps * (i) / N;};
break;
case 'periods':
f = function prd(i){return N / (sps * (i));};
}
return this._forloop(this.dim(F1, F2), function(i){
return f.call(null, i);
});
};

FFT.prototype.amplitude = function amplitude(F1, F2) {
return this._convertTo('amplitude', F1, F2, 1);
FFT.prototype = {
dim: function dim(F1, F2){
var N = F1.length;

//limited 2^x
while(true){
if( !(N & (N-1)) ){
break;
}
N = N + 1;
F1.push(0);
F2.push(0);
}
return N;
},
_forloop: function _forloop(N, callback){
var m = N / 2;
var ans = [];
for (var i = 0; i < m; i = i + 1){
ans.push( callback.call(null, i) );
}
return ans;
},
_convertTo: function _convertTo(unit, F1, F2, samplingrate){
var sps = samplingrate || this.settings.samplingrate;
var f;
switch(unit){
case 'amplitude':
f = function amp(i){return Math.sqrt(F1[i] * F1[i] + F2[i] * F2[i]);};
break;
case 'power':
f = function pow(i){return F1[i] * F1[i] + F2[i] * F2[i];};
break;
case 'phase':
f = function phs(i){return Math.atan2(F2[i], F1[i]);};
break;
case 'frequencies':
f = function freq(i){return sps * (i) / N;};
break;
case 'periods':
f = function prd(i){return N / (sps * (i));};
}
return this._forloop(this.dim(F1, F2), function(i){
return f.call(null, i);
});
},
amplitude: function amplitude(F1, F2) {
return this._convertTo('amplitude', F1, F2, 1);
},
power: function power(F1, F2) {
return this._convertTo('power', F1, F2, 1);
},
phase: function phase(F1, F2) {
return this._convertTo('phase', F1, F2, 1);
},
frequencies: function frequencies(F1, F2, samplingrate) {
return this._convertTo('frequencies', F1, F2, samplingrate);
},
periods: function periods(F1, F2, samplingrate) {
return this._convertTo('periods', F1, F2, samplingrate);
},
calc:function calc(SW, F1, F2) {
var WN, T, A1, A2, B1, B2, W1, W2, C, S;
//integers
var m, i, j, k, l, jl, jm, m1, kl;

N = this.dim(F1, F2);
INDEX = Math.log2(N);
DX = 1/N;

var PAI = 3.14159265358979;
WN = 2 * PAI / N;
m = N;

for (l = 0; l < INDEX; l = l + 1){
T = 0;
m1 = m;
m = m / 2;

for (k = 0; k < m ; k = k + 1){
kl = k - m1;
C = Math.cos(T);
S = -SW * Math.sin(T);
T = T + WN;

for (j = m1 ; j < N + 1 ; j = j + m1){
jl = j + kl;
jm = jl + m;
A1 = F1[jl];
A2 = F1[jm];
B1 = F2[jl];
B2 = F2[jm];
F1[jl] = A1 + A2;
F2[jl] = B1 + B2;
F1[jm] = (A1 - A2) * C + (B1 - B2) * S;
F2[jm] = (B1 - B2) * C - (A1 - A2) * S;
}
}
WN = 2 * WN;
}

j = 0;
for (i = 0 ; i < N - 1; i = i + 1){

if ( i < j ){
W1 = F1[j];
W2 = F2[j];
F1[j] = F1[i];
F2[j] = F2[i];
F1[i] = W1;
F2[i] = W2;
}

k = N / 2;
while (true){
if (k > j){break;}
j = j - k;
k = k / 2;
}
j = j + k;
}

if (SW < 0){
for (i =0; i < N; i = i + 1){
F1[i] = DX * F1[i];
F2[i] = DX * F2[i];
}
}

return [F1, F2];
}
};

FFT.prototype.power = function power(F1, F2) {
return this._convertTo('power', F1, F2, 1);
};

FFT.prototype.phase = function phase(F1, F2) {
return this._convertTo('phase', F1, F2, 1);
};

FFT.prototype.frequencies = function frequencies(F1, F2, samplingrate) {
return this._convertTo('frequencies', F1, F2, samplingrate);
};

FFT.prototype.periods = function periods(F1, F2, samplingrate) {
return this._convertTo('periods', F1, F2, samplingrate);
};

FFT.prototype.calc = function calc(SW, F1, F2) {

var WN, T, A1, A2, B1, B2, W1, W2, C, S;
//integers
var m, i, j, k, l, jl, jm, m1, kl;

N = this.dim(F1, F2);
INDEX = Math.log2(N);
DX = 1/N;

var PAI = 3.14159265358979;
WN = 2 * PAI / N;
m = N;

for (l = 0; l < INDEX; l = l + 1){
T = 0;
m1 = m;
m = m / 2;

for (k = 0; k < m ; k = k + 1){
kl = k - m1;
C = Math.cos(T);
S = -SW * Math.sin(T);
T = T + WN;

for (j = m1 ; j < N + 1 ; j = j + m1){
jl = j + kl;
jm = jl + m;
A1 = F1[jl];
A2 = F1[jm];
B1 = F2[jl];
B2 = F2[jm];
F1[jl] = A1 + A2;
F2[jl] = B1 + B2;
F1[jm] = (A1 - A2) * C + (B1 - B2) * S;
F2[jm] = (B1 - B2) * C - (A1 - A2) * S;
}
}
WN = 2 * WN;
}

j = 0;
for (i = 0 ; i < N - 1; i = i + 1){

if ( i < j ){
W1 = F1[j];
W2 = F2[j];
F1[j] = F1[i];
F2[j] = F2[i];
F1[i] = W1;
F2[i] = W2;
}

k = N / 2;
while (true){
if (k > j){break;}
j = j - k;
k = k / 2;
}
j = j + k;
}

if (SW < 0){
for (i =0; i < N; i = i + 1){
F1[i] = DX * F1[i];
F2[i] = DX * F2[i];
}
}

return [F1, F2];
};

//export
return FFT;
}));
20 changes: 10 additions & 10 deletions jquery.fft.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
* Released under the MIT license
*/
(function(a,b){if(typeof define==="function"&&define.amd){define(b);
}else{if(typeof exports==="object"){module.exports=b();}else{a.FFT=b();}}}(this,function(){var c=function(k){this.settings=k||{samplingrate:1};};c.prototype.dim=function f(l,k){var m=l.length;
while(true){if(!(m&(m-1))){break;}m=m+1;l.push(0);k.push(0);}return m;};c.prototype._forloop=function i(o,p){var k=o/2;var l=[];for(var n=0;n<k;n=n+1){l.push(p.call(null,n));
}return l;};c.prototype._convertTo=function g(u,q,p,t){var k=t||this.settings.samplingrate;var r;switch(u){case"amplitude":r=function n(v){return Math.sqrt(q[v]*q[v]+p[v]*p[v]);
}else{if(typeof exports==="object"){module.exports=b();}else{a.FFT=b();}}}(this,function(){var c=function(k){this.settings=k||{samplingrate:1};};c.prototype={dim:function f(l,k){var m=l.length;
while(true){if(!(m&(m-1))){break;}m=m+1;l.push(0);k.push(0);}return m;},_forloop:function i(o,p){var k=o/2;var l=[];for(var n=0;n<k;n=n+1){l.push(p.call(null,n));
}return l;},_convertTo:function g(u,q,p,t){var k=t||this.settings.samplingrate;var r;switch(u){case"amplitude":r=function n(v){return Math.sqrt(q[v]*q[v]+p[v]*p[v]);
};break;case"power":r=function s(v){return q[v]*q[v]+p[v]*p[v];};break;case"phase":r=function m(v){return Math.atan2(p[v],q[v]);};break;case"frequencies":r=function l(v){return k*(v)/N;
};break;case"periods":r=function o(v){return N/(k*(v));};}return this._forloop(this.dim(q,p),function(v){return r.call(null,v);});};c.prototype.amplitude=function b(l,k){return this._convertTo("amplitude",l,k,1);
};c.prototype.power=function a(l,k){return this._convertTo("power",l,k,1);};c.prototype.phase=function j(l,k){return this._convertTo("phase",l,k,1);};c.prototype.frequencies=function d(l,k,m){return this._convertTo("frequencies",l,k,m);
};c.prototype.periods=function h(l,k,m){return this._convertTo("periods",l,k,m);};c.prototype.calc=function e(u,I,F){var K,r,p,o,v,t,x,w,y,s;var z,G,D,B,A,H,E,n,q;
N=this.dim(I,F);INDEX=Math.log2(N);DX=1/N;var J=3.14159265358979;K=2*J/N;z=N;for(A=0;A<INDEX;A=A+1){r=0;n=z;z=z/2;for(B=0;B<z;B=B+1){q=B-n;y=Math.cos(r);
s=-u*Math.sin(r);r=r+K;for(D=n;D<N+1;D=D+n){H=D+q;E=H+z;p=I[H];o=I[E];v=F[H];t=F[E];I[H]=p+o;F[H]=v+t;I[E]=(p-o)*y+(v-t)*s;F[E]=(v-t)*y-(p-o)*s;}}K=2*K;
}D=0;for(G=0;G<N-1;G=G+1){if(G<D){x=I[D];w=F[D];I[D]=I[G];F[D]=F[G];I[G]=x;F[G]=w;}B=N/2;while(true){if(B>D){break;}D=D-B;B=B/2;}D=D+B;}if(u<0){for(G=0;
G<N;G=G+1){I[G]=DX*I[G];F[G]=DX*F[G];}}return[I,F];};return c;}));
};break;case"periods":r=function o(v){return N/(k*(v));};}return this._forloop(this.dim(q,p),function(v){return r.call(null,v);});},amplitude:function b(l,k){return this._convertTo("amplitude",l,k,1);
},power:function a(l,k){return this._convertTo("power",l,k,1);},phase:function j(l,k){return this._convertTo("phase",l,k,1);},frequencies:function d(l,k,m){return this._convertTo("frequencies",l,k,m);
},periods:function h(l,k,m){return this._convertTo("periods",l,k,m);},calc:function e(u,I,F){var K,r,p,o,v,t,x,w,y,s;var z,G,D,B,A,H,E,n,q;N=this.dim(I,F);
INDEX=Math.log2(N);DX=1/N;var J=3.14159265358979;K=2*J/N;z=N;for(A=0;A<INDEX;A=A+1){r=0;n=z;z=z/2;for(B=0;B<z;B=B+1){q=B-n;y=Math.cos(r);s=-u*Math.sin(r);
r=r+K;for(D=n;D<N+1;D=D+n){H=D+q;E=H+z;p=I[H];o=I[E];v=F[H];t=F[E];I[H]=p+o;F[H]=v+t;I[E]=(p-o)*y+(v-t)*s;F[E]=(v-t)*y-(p-o)*s;}}K=2*K;}D=0;for(G=0;G<N-1;
G=G+1){if(G<D){x=I[D];w=F[D];I[D]=I[G];F[D]=F[G];I[G]=x;F[G]=w;}B=N/2;while(true){if(B>D){break;}D=D-B;B=B/2;}D=D+B;}if(u<0){for(G=0;G<N;G=G+1){I[G]=DX*I[G];
F[G]=DX*F[G];}}return[I,F];}};return c;}));

0 comments on commit f48e8a7

Please sign in to comment.