A TensorFlow-based Sinusoidal Neural Network (SNN) for signal decomposition, spectral analysis, and signal reconstruction.
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
pip install sinusoidalnnBefore writing any code, it is recommended to explore the example project.
Inside the repository:
examples/
├── demo.py
├── signal_value.py
├── signal_original.csv
Run:
python demo.pyThe demo will:
- Load a signal from CSV
- Train a SinusoidalNN model
- Save the trained model
- Extract the learned spectrum
- Plot the spectrum
- Reconstruct the signal
- 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. To demonstrate loading model and print the signal value at arbitrary time, run signal_value.py instead.
from sinusoidalnn import SNNimport pandas as pd
df = pd.read_csv("signal.csv")
t = df["Time"]
y = df["Signal"]snn = SNN(
max_modes=10000,
optimizer="adam",
learning_rate=1e-5,
verbose=1
)snn.fit_signal(
t,
y,
epochs=256
)spectrum = snn.return_spectrum()Example:
Frequencies Phase shift Amplitudes
0 49.99 0.12 0.998
1 120.03 -0.31 0.502
...
signal = snn.reconstruct_signal(
duration=1.0,
sample_rate=1000
)Example Output:
Time Signal
0 0.0000 0.0123
1 0.0010 0.5234
...
snn.save_model(
"my_model.h5"
)snn.load_SNN_model(
"my_model.h5"
)snn.signal_value(
[[2.5]]
)from sinusoidalnn import zeroing_baseline
y = zeroing_baseline(y)Removes DC offset from a signal by subtracting its mean value.
SinusoidalNN supports:
- SGD
- RMSprop
- Adam
- AdamW
- Adagrad
- Adadelta
- Adamax
- Nadam
- FTRL
Example:
snn = SNN(
optimizer="adamw"
)SNN(
max_modes=10000,
use_phase_shift=True,
optimizer="adam",
learning_rate=1e-4,
verbose=2
)fit_signal(
t,
y,
epochs=256
)Train the model on a signal.
return_spectrum(
positive_freqs_only=True,
abs_amplitudes=True
)Extract learned frequencies, amplitudes, and phase shifts.
reconstruct_signal(
duration=5.0,
sample_rate=44100
)Generate a reconstructed signal using the learned sinusoidal representation.
save_model(path)Save the TensorFlow model.
load_SNN_model(path)Load a previously saved model.
snn.signal_value(
[[2.5]]
)Returns signal value at arbitrary time.
- TensorFlow
- NumPy
- Pandas
- Matplotlib
MIT License
https://github.com/jovan-AIcoder/SinusoidalNN
Jovan
If you use SinusoidalNN in research, academic work, scientific reports, or publications, please cite the repository and acknowledge the package appropriately.
