Skip to main content

TensorFlow-based Sinusoidal Neural Network for signal decomposition, spectral analysis, and signal reconstruction

Project description

SinusoidalNN

logo

A TensorFlow-based Sinusoidal Neural Network (SNN) for signal decomposition, spectral analysis, and signal reconstruction.


Overview

SinusoidalNN is a Python package that represents a signal as a trainable superposition of sinusoidal components.

Instead of using a conventional Fourier Transform, SinusoidalNN learns frequencies, amplitudes, and phase shifts directly through gradient descent using TensorFlow.

Signal representation:

y(t) = Σ Ai sin(2πfi t + φi)

where:

  • fi = learned frequency
  • Ai = learned amplitude
  • φi = learned phase shift

The package provides:

  • Signal fitting
  • Spectrum extraction
  • Signal reconstruction
  • Model saving and loading
  • Signal preprocessing
  • TensorFlow optimizer support

Installation

pip install sinusoidalnn

Try the Demo First

Before writing any code, it is recommended to explore the example project.

Inside the repository:

examples/

├── demo.py
├── signal_original.csv

Run:

python demo.py

The demo will:

  1. Load a signal from CSV
  2. Train a SinusoidalNN model
  3. Save the trained model
  4. Extract the learned spectrum
  5. Plot the spectrum
  6. Reconstruct the signal
  7. Save the reconstructed signal

Generated files:

model_demo.h5

spectrum.csv
spectrum.png

signal_reconstructed.csv

signal_comparison.png

This is the fastest way to understand how SinusoidalNN works.


Quick Start

Import Package

from sinusoidalnn import SNN

Load Signal

import pandas as pd

df = pd.read_csv("signal.csv")

t = df["Time"]
y = df["Signal"]

Create Model

snn = SNN(
    max_modes=1000,
    optimizer="adam",
    learning_rate=1e-4,
    verbose=1
)

Fit Signal

snn.fit_signal(
    t,
    y,
    epochs=256
)

Extract Spectrum

spectrum = snn.return_spectrum()

Example:

   Frequencies  Phase shift  Amplitudes
0      49.99       0.12        0.998
1     120.03      -0.31        0.502
...

Reconstruct Signal

signal = snn.reconstruct_signal(
    duration=1.0,
    sample_rate=1000
)

Output:

       Time    Signal
0    0.0000   0.0123
1    0.0010   0.5234
...

Save Model

snn.save_model(
    "my_model.h5"
)

Load Model

snn.load_SNN_model(
    "my_model.h5"
)

Signal Preprocessing

Baseline Removal

from sinusoidalnn import zeroing_baseline

y = zeroing_baseline(y)

Removes DC offset from a signal by subtracting its mean value.


Supported Optimizers

SinusoidalNN supports:

  • SGD
  • RMSprop
  • Adam
  • AdamW
  • Adagrad
  • Adadelta
  • Adamax
  • Nadam
  • FTRL

Example:

snn = SNN(
    optimizer="adamw"
)

API Reference

SNN

SNN(
    max_modes=10000,
    use_phase_shift=True,
    optimizer="adam",
    learning_rate=1e-4,
    verbose=2
)

fit_signal

fit_signal(
    t,
    y,
    epochs=256
)

Train the model on a signal.


return_spectrum

return_spectrum(
    positive_freqs_only=True,
    abs_amplitudes=True
)

Extract learned frequencies, amplitudes, and phase shifts.


reconstruct_signal

reconstruct_signal(
    duration=5.0,
    sample_rate=44100
)

Generate a reconstructed signal using the learned sinusoidal representation.


save_model

save_model(path)

Save the TensorFlow model.


load_SNN_model

load_SNN_model(path)

Load a previously saved model.


Dependencies

  • TensorFlow
  • NumPy
  • Pandas
  • Matplotlib

License

MIT License


Repository

https://github.com/jovan-AIcoder/SinusoidalNN


Author

Jovan


Citation

If you use SinusoidalNN in research, academic work, scientific reports, or publications, please cite the repository and acknowledge the package appropriately.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sinusoidalnn-1.0.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sinusoidalnn-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file sinusoidalnn-1.0.0.tar.gz.

File metadata

  • Download URL: sinusoidalnn-1.0.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for sinusoidalnn-1.0.0.tar.gz
Algorithm Hash digest
SHA256 26c15a5d83009b51b56f7df3c4bd18a3ea6182204052e1972e0b9f9acea2ca50
MD5 350d97330d2e5ec942f9e8a1900444c6
BLAKE2b-256 6e855c4a81adf95c7cb32690d32dd51f819ec4d340fdeda61ada53072f5fdf45

See more details on using hashes here.

File details

Details for the file sinusoidalnn-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sinusoidalnn-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for sinusoidalnn-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 041bc97d463aef9db652faceada3ce2f9e3567662e5a0e2dc48c239ab2eb5a4e
MD5 a262f76bde9535d6eabcc5fede4b577f
BLAKE2b-256 bddb0e05cdde920a428cfe5abef2a896c7c57d6b16d68dfa01236edb38c2e9b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page