# Mathematical library for calculating the EEG signal spectrum

The main functionality is the calculation of raw spectrum values and the calculation of EEG spectrum values. Working with the library is possible in iterative mode (adding new data to the internal buffer, calculating spectrum values) and in one-time spectrum calculation mode for a given array. When working in the iterative mode, the spectrum is calculated with the frequency set during initialization.

The spectrum is calculated by FFT method. It can be calculated separately for any signal, both raw and after filtering.

## Install

Supported platforms:

- Android
- iOS
- MacOS
- Windows
- Linux

Run this command with Flutter:

```
$ flutter pub add spectrum_lib
```

Or install it manually:

- add package to dependencies:

```
dependencies:
spectrum_lib: ^1.0.0
```

- run
`flutter pub get`

command

Then use it in your Dart code:

```
import 'package:spectrum_lib/spectrum_lib.dart';
```

SDK uses a runtime permission to bluetooth and location, so you need to ask user about it.

The latest version is `1.0.0`

.

Samples for BrainBit Samples for Callibri

## Initialization

### Main parameters

- Raw signal sampling frequency. Need to be >= 1.
- Spectrum calculation frequency. Need to be <= 16 kHz.
- Spectrum calculation window length. Need to be <= signal sampling frequency.

### Optional parameters

- Upper bound of frequencies for spectrum calculation. Default value is sampling_rate / 2.
- Normalization of the EEG spectrum by the width of the wavebands. Disabled by default.
- Weight coefficients for alpha, beta, theta, gamma, delta waves. By default has 1.0 value.

## Creation

```
int samplingRate = 250; // raw signal sampling frequency
int fftWindow = 1000; // spectrum calculation window length
int processWinRate = 5; // spectrum calculation frequency
final math = SpectrumLib(samplingRate, fftWindow, processWinRate);
```

## Optional initialization

- Additional spectrum settings:

```
int bordFrequency = 50; // upper bound of frequencies for spectrum calculation
bool normalizeSpectByBandwidth = true; // normalization of the EEG spectrum by the width of the wavebands
math.initParams(bordFrequency, normalizeSpectByBandwidth);
```

- Waves coefficients:

```
double deltaCoef = 0.0;
double thetaCoef = 1.0;
double alphaCoef = 1.0;
double betaCoef = 1.0;
double gammaCoef = 0.0;
math.setWavesCoeffs(deltaCoef, thetaCoef, alphaCoef, betaCoef, gammaCoef);
```

- Setting the smoothing of the spectrum calculation by Henning (by default) or Hemming window:

```
math.setHammingWinSpectrum(); // by Hanning (by default)
math.setHanningWinSpectrum(); // by Hamming
```

## Initializing a data array for transfer to the library

Array of double values with length less or equals then 15 * signal sampling frequency.

## Types

#### RawSpectrumData

Structure containing the raw spectrum values (with boundary frequency taken into library).

Fields:

- all_bins_nums - Integer value. Number of FFT bars. Contained only in the C++ interface.
- all_bins_values - Double array. Raw FFT bars values.
- total_raw_pow - Double value. Total raw spectrum power.

#### WavesSpectrumData

Structure containing the waves values.

Absolute frequency values (double type):

- delta_raw
- theta_raw
- alpha_raw
- beta_raw
- gamma_raw

Relative (percent) values (double type):

- delta_rel
- theta_rel
- alpha_rel
- beta_rel
- gamma_rel

## FFT band resolution

The library automatically matches the optimal buffer length (degree 2) to calculate the FFT during initialization, depending on the specified window length. Receiving resolution for the FFT bands (number of FFT bands per 1 Hz):

```
math.getFFTBinsFor1Hz();
```

## Spectrum calculation in iterative mode

- Adding and process data:

```
List<double> data = List.empty();
math.pushAndProcessData(data);
```

- Getting the results:

```
List<RawSpectrumData> rawSpectrumData = math.readRawSpectrumInfoArr();
List<WavesSpectrumData> wavesSpectrumData = math.readWavesSpectrumInfoArr();
```

- Updating the number of new samples. Is necessary for correct calculation of elements in the array of obtained structures, if a large portion of data is added to the library all at once.

```
math.setNewSampleSize();
```

## Spectrum calculation for a single array

- Compute spectrum:

```
List<double> data = List.empty();
math.computeSpectrum(data);
```

- Getting the results.
`WavesSpectrumData`

rhythm values are presented as percentages and can take values from 0 to 100. If you use rhythm values in raw values, then the dimension will be microvolts per square root of hertz. Rhythm values are calculated relative to the total power, i.e., the sum of raw frequency values.

```
RawSpectrumData rawSpectrumData = math.readRawSpectrumInfo();
WavesSpectrumData wavesSpectrumData = math.readWavesSpectrumInfo();
```

## Finishing work with the library

```
math.dispose();
```