Skip to content

Commit

Permalink
FFT class first test
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrypula committed Apr 19, 2018
1 parent d4c85b6 commit bea5556
Show file tree
Hide file tree
Showing 21 changed files with 231 additions and 155 deletions.
16 changes: 8 additions & 8 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
- [DONE] finish physical layer example
- finish data link layer simple example
- move all modules to main NPM package
- switch all examples to the one lib file instead of development mode that loads all scripts
- [DONE for all examples related to NPM package] switch all examples to the one lib file instead of development mode that loads all scripts
- finish transport layer example
- DataChunk listeners
- segment listeners
- example page itself
- finish Audio Chat example
- replace Gulp by WebPack
- configure unit tests from console
- finish Audio Chat example
- [DONE in PR] replace Gulp by WebPack
- [DONE in PR] configure unit tests from console
- [DONE] remove external google fonts
- improove website loading time in general
- [DONE] improove website loading time in general

## Physical Layer, Web Audio API and DSP stuff

Expand All @@ -32,16 +32,16 @@
- SYNC setting 48/44.1 will no longer be needed in the Layers above Physical like Data Link Layer
- custom FFT implementation
- resampler
- FIR filter
- FIR filter

## Data Link Layer

- remove two way sync
- look for frames in two FSK streams
- SYNC will not be required
- SYNC will not be required

## Old todos (might be not valid anymore)

- DFT simple fix (code refactor)
- full example DFT fix
- add sampleRate input
Expand Down
2 changes: 1 addition & 1 deletion src/common/list/list.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface IList<T> {
// appendArrayEvenIfFull(value: T): IList<T>;
takeFirst(): T;
takeLast(): T;
fillWith(value: T, size: number): IList<T>;
fillWith(value: T, size?: number): IList<T>;
isFull(): boolean;
isEmpty(): boolean;
setSizeMax(sizeMax: number): IList<T>;
Expand Down
5 changes: 2 additions & 3 deletions src/common/list/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,8 @@ describe('List', () => {
expect(list.getSize()).toBe(3);
try {
list.append(4);
} catch (e) {
/* tslint:disable:no-empty */
}
/* tslint:disable-next-line:no-empty */
} catch (e) { }
expect(list.getSize()).toBe(3);
});

Expand Down
3 changes: 3 additions & 0 deletions src/dependency-injection.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { LIST_FACTORY } from './common/list/di-token';
import ListFactory from './common/list/list-factory';
import { SIMPLE_MATH } from './common/simple-math/di-token';
import SimpleMath from './common/simple-math/simple-math';
import ComplexListUtil from './dsp/complex-list-util/complex-list-util';
import { COMPLEX_LIST_UTIL } from './dsp/complex-list-util/di-token';
import ComplexFactory from './dsp/complex/complex-factory';
import { COMPLEX_FACTORY } from './dsp/complex/di-token';
import { DSP_MODULE } from './dsp/di-token';
Expand All @@ -22,6 +24,7 @@ injector.registerService(SIMPLE_MATH, SimpleMath);
injector.registerService(COMMON_MODULE, CommonModule);

injector.registerService(COMPLEX_FACTORY, ComplexFactory);
injector.registerService(COMPLEX_LIST_UTIL, ComplexListUtil);
injector.registerService(FOURIER_TRANSFORM, Fft);
injector.registerService(DSP_MODULE, DspModule);

Expand Down
28 changes: 0 additions & 28 deletions src/di.ts

This file was deleted.

29 changes: 29 additions & 0 deletions src/dsp/complex-list-util/complex-list-util.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

import { IListFactory } from '../../common/list/list-factory.interface';
import { IList } from '../../common/list/list.interface';
import { IComplexFactory } from '../complex/complex-factory.interface';
import { IComplex, IComplexDto } from '../complex/complex.interface';

type IComplexListDto = IComplexDto[];

interface IComplexListUtil {
fromDto(complexListDto: IComplexListDto): IList<IComplex>;
// fromRawIQ(dto: number[]): IList<IComplex>;
toDto(complexList: IList<IComplex>): IComplexListDto;
// toRawIQ(list: IList<IComplex>): number[];
// isEqual(a: IList<IComplex>, b: IList<IComplex>): boolean;
}

interface IComplexListUtilStatic {
new(
complexFactory: IComplexFactory,
listFactory: IListFactory
): IComplexListUtil;
}

export {
IComplexListDto,
IComplexListUtil,
IComplexListUtilStatic
};
50 changes: 50 additions & 0 deletions src/dsp/complex-list-util/complex-list-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

import { staticImplements } from 'rr-tsdi';

import { LIST_FACTORY } from '../../common/list/di-token';
import { IListFactory } from '../../common/list/list-factory.interface';
import { IList } from '../../common/list/list.interface';
import { IComplexFactory } from '../complex/complex-factory.interface';
import { IComplex, IComplexDto } from '../complex/complex.interface';
import { COMPLEX_FACTORY } from '../complex/di-token';
import { IComplexListDto, IComplexListUtil, IComplexListUtilStatic } from './complex-list-util.interface';

@staticImplements<IComplexListUtilStatic>()
class ComplexListUtil implements IComplexListUtil {
public static $inject: string[] = [
COMPLEX_FACTORY,
LIST_FACTORY
];

constructor(
protected complexFactory: IComplexFactory,
protected listFactory: IListFactory
) {
}

public fromDto(complexListDto: IComplexListDto): IList<IComplex> {
const tmp = complexListDto.map((complexDto: IComplexDto) => {
return this.complexFactory.createFromDto(complexDto);
});

return this.listFactory.createFromArray<IComplex>(tmp);
}

public toDto(complexList: IList<IComplex>): IComplexListDto {
return complexList
.toArray()
.map(
(value: IComplex): IComplexDto => {
/* tslint:disable:object-literal-sort-keys */
return {
real: value.getReal(),
imaginary: value.getImaginary()
};
/* tslint:enable:object-literal-sort-keys */
}
);
}
}

export default ComplexListUtil;
12 changes: 0 additions & 12 deletions src/dsp/complex-list-util/complex-list.interface.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/dsp/complex-list-util/di-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

export const COMPLEX_LIST_UTIL = 'COMPLEX_LIST_UTIL';
7 changes: 5 additions & 2 deletions src/dsp/complex/complex-factory.interface.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// Copyright (c) 2015-2018 Robert Rypuła - https://audio-network.rypula.pl

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

interface IComplexFactory {
create(real?: number, imaginary?: number): IComplex;
createPolar(unitAngle?: number, magnitude?: number): IComplex;
createFromDto(complexDto: IComplexDto): IComplex;
}

interface IComplexFactoryStatic {
new(simpleMath: ISimpleMath): IComplexFactory;
new(
simpleMath: ISimpleMath
): IComplexFactory;
}

export {
Expand Down
10 changes: 9 additions & 1 deletion src/dsp/complex/complex-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SIMPLE_MATH } from '../../common/simple-math/di-token';
import SimpleMath from '../../common/simple-math/simple-math';
import Complex from './complex';
import ComplexFactory from './complex-factory';

import { IComplexDto } from './complex.interface';
import { COMPLEX_FACTORY } from './di-token';

describe('ComplexFactory', () => {
Expand Down Expand Up @@ -52,4 +52,12 @@ describe('ComplexFactory', () => {
expect(complex.getImaginary()).toBeCloseTo(1.414214, NUMBER_OF_DIGITS);
});

it('should create instance of Complex class basing on dto', () => {
const dto: IComplexDto = { real: 12, imaginary: -32 };
const complex: Complex = complexFactory.createFromDto(dto);

expect(complex).toBeInstanceOf(Complex);
expect(complex.getReal()).toBe(dto.real);
expect(complex.getImaginary()).toBe(dto.imaginary);
});
});
15 changes: 14 additions & 1 deletion src/dsp/complex/complex-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SIMPLE_MATH } from '../../common/simple-math/di-token';
import { ISimpleMath } from './../../common/simple-math/simple-math.interface';
import Complex from './complex';
import { IComplexFactory, IComplexFactoryStatic } from './complex-factory.interface';
import { IComplexDto } from './complex.interface';

@staticImplements<IComplexFactoryStatic>()
class ComplexFactory implements IComplexFactory {
Expand All @@ -19,7 +20,11 @@ class ComplexFactory implements IComplexFactory {
}

public create(real: number = 0, imaginary: number = 0): Complex {
return new Complex(this.simpleMath, real, imaginary);
return new Complex(
this.simpleMath,
real,
imaginary
);
}

public createPolar(unitAngle: number = 0, magnitude: number = 1): Complex {
Expand All @@ -30,6 +35,14 @@ class ComplexFactory implements IComplexFactory {
magnitude * this.simpleMath.sin(radian)
);
}

public createFromDto(complexDto: IComplexDto): Complex {
return new Complex(
this.simpleMath,
complexDto.real,
complexDto.imaginary
);
}
}

export default ComplexFactory;
13 changes: 12 additions & 1 deletion src/dsp/complex/complex.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

interface IComplexDto {
real: number;
imaginary: number;
}

interface IComplex {
clone(): IComplex;
swap(): IComplex;
Expand All @@ -16,13 +21,19 @@ interface IComplex {
getMagnitude(): number;
getUnitAngle(): number;
normalize(): IComplex;
toDto(): IComplexDto;
}

interface IComplexStatic {
new(simpleMath: ISimpleMath, real: number, imaginary: number): IComplex;
new(
simpleMath: ISimpleMath,
real: number,
imaginary: number
): IComplex;
}

export {
IComplexDto,
IComplex,
IComplexStatic
};
8 changes: 8 additions & 0 deletions src/dsp/complex/complex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import SimpleMath from './../../common/simple-math/simple-math';
import Complex from './complex';
import { IComplexDto } from './complex.interface';

describe('Complex', () => {
const NUMBER_OF_DIGITS = 6;
Expand Down Expand Up @@ -154,4 +155,11 @@ describe('Complex', () => {
expect(complexNormalized.getMagnitude()).toBeCloseTo(UNIT, NUMBER_OF_DIGITS);
expect(complex).toBe(complexNormalized);
});

it('should properly return DTO', () => {
const complexDto: IComplexDto = complex.toDto();

expect(complexDto.real).toBe(complex.getReal());
expect(complexDto.imaginary).toBe(complex.getImaginary());
});
});
11 changes: 10 additions & 1 deletion src/dsp/complex/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { staticImplements } from 'rr-tsdi';

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

@staticImplements<IComplexStatic>()
class Complex implements IComplex {
Expand Down Expand Up @@ -137,6 +137,15 @@ class Complex implements IComplex {

return this;
}

public toDto(): IComplexDto {
/* tslint:disable:object-literal-sort-keys */
return {
real: this.real,
imaginary: this.imaginary
};
/* tslint:enable:object-literal-sort-keys */
}
}

export default Complex;

0 comments on commit bea5556

Please sign in to comment.