Skip to content

Commit

Permalink
Adds staticImplements check, FFT class update
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrypula committed Apr 15, 2018
1 parent d375c80 commit 849fb81
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/common/simple-math/simple-math.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

import { ISimpleMath } from './simple-math.interface';
import { staticImplements } from 'rr-tsdi';

import { ISimpleMath, ISimpleMathStatic } from './simple-math.interface';

@staticImplements<ISimpleMathStatic>()
class SimpleMath implements ISimpleMath {
public getPi(): number {
return Math.PI;
Expand Down
5 changes: 4 additions & 1 deletion src/dsp/complex/complex-factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

import { staticImplements } from 'rr-tsdi';

import { SIMPLE_MATH } from '../../common/simple-math/di-token';
import { ISimpleMath } from './../../common/simple-math/simple-math.interface';
import Complex from './complex';
import { IComplexFactory } from './complex-factory.interface';
import { IComplexFactory, IComplexFactoryStatic } from './complex-factory.interface';

@staticImplements<IComplexFactoryStatic>()
class ComplexFactory implements IComplexFactory {
public static $inject: string[] = [
SIMPLE_MATH
Expand Down
5 changes: 4 additions & 1 deletion src/dsp/complex/complex.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

import { staticImplements } from 'rr-tsdi';

import { ISimpleMath } from './../../common/simple-math/simple-math.interface';
import { IComplex } from './complex.interface';
import { IComplex, IComplexStatic } from './complex.interface';

@staticImplements<IComplexStatic>()
class Complex implements IComplex {
// TODO probably it's bad to have reference to 'simpleMath'
// in each Complex object but it's how DI works...
Expand Down
1 change: 1 addition & 0 deletions src/dsp/fft/fft.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IComplex } from '../complex/complex.interface';

interface IFft {
forward(input: IComplex[]): IComplex[];
inverse(input: IComplex[]): IComplex[];
}

interface IFftStatic {
Expand Down
66 changes: 66 additions & 0 deletions src/dsp/fft/fft.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

import { Injector } from 'rr-tsdi';

import { SIMPLE_MATH } from '../../common/simple-math/di-token';
import SimpleMath from '../../common/simple-math/simple-math';
import ComplexFactory from '../complex/complex-factory';
import { IComplexFactory } from '../complex/complex-factory.interface';
import { IComplex } from '../complex/complex.interface';
import { COMPLEX_FACTORY } from '../complex/di-token';
import { FFT } from './di-token';
import Fft from './fft';
import { IFft } from './fft.interface';

describe('Complex', () => {
let fft: IFft;
let complexFactory: IComplexFactory;

beforeEach(() => {
const injector = new Injector();

injector.registerService(SIMPLE_MATH, SimpleMath);
injector.registerService(COMPLEX_FACTORY, ComplexFactory);
injector.registerService(FFT, Fft);
fft = injector.get(FFT);
complexFactory = injector.get(COMPLEX_FACTORY);
});

// TODO refactor it
xit('should', () => {
const input = [
complexFactory.createPolar(0.00, 1),
complexFactory.createPolar(0.25, 1),
complexFactory.createPolar(0.50, 1),
complexFactory.createPolar(0.75, 1)
];
const output = fft
.forward(input)
.map((item: IComplex) => {
return {
imaginary: item.getImaginary(),
real: item.getReal()
};
});

expect(output).toEqual([
{
imaginary: 0,
real: 0
},
{
imaginary: 0,
real: 0
},
{
imaginary: 0,
real: 0
},
{
imaginary: 0,
real: 0
}
]);
});

});
46 changes: 21 additions & 25 deletions src/dsp/fft/fft.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

import { staticImplements } from 'rr-tsdi';

import { IComplexFactory } from '../complex/complex-factory.interface';
import { IComplex } from '../complex/complex.interface';
import { COMPLEX_FACTORY } from '../complex/di-token';
import { IFft } from './fft.interface';
import { IFft, IFftStatic } from './fft.interface';

@staticImplements<IFftStatic>()
class Fft implements IFft {
public static $inject: string[] = [
COMPLEX_FACTORY
Expand All @@ -16,59 +19,52 @@ class Fft implements IFft {
}

public forward(input: IComplex[]): IComplex[] {
return input;
/*
var
n = input.length,
nHalf,
even,
odd,
output = [],
wnkMultiplied,
wnk,
k,
unitAngle;
const n = input.length;
let nHalf: number;
let even: IComplex[];
let odd: IComplex[];
const output: IComplex[] = [];
let wnkMultiplied: IComplex;
let wnk: IComplex;
let k: number;
let unitAngle: number;

if (n === 1) {
return input;
}

// even and odd parts
even = Fft.forward(Fft.$$getHalf(input, 0));
odd = Fft.forward(Fft.$$getHalf(input, 1));
even = this.forward(this.getHalf(input, 0));
odd = this.forward(this.getHalf(input, 1));

// combine
output.length = n;
nHalf = n / 2;
for (k = 0; k < nHalf; k++) {
unitAngle = -k / n;
wnk = Complex.polar(unitAngle);
wnk = this.complexFactory.createPolar(unitAngle);
wnkMultiplied = wnk.clone().multiply(odd[k]);
output[k] = even[k].clone().add(wnkMultiplied);
output[nHalf + k] = even[k].clone().subtract(wnkMultiplied);
}

return output;
*/
}

/*
Fft.inverse = function (input) {
var
output = [],
i;
public inverse(input: IComplex[]): IComplex[] {
let output: IComplex[] = [];
let i: number;

for (i = 0; i < input.length; i++) {
output.push(input[i].clone().swap());
}
output = Fft.forward(output);
output = this.forward(output);
for (i = 0; i < output.length; i++) {
output[i].swap().divideScalar(output.length);
}

return output;
};
*/
}

private getHalf(list: IComplex[], offset: number): IComplex[] {
const listHalf: IComplex[] = [];
Expand Down

0 comments on commit 849fb81

Please sign in to comment.