Neural Networks
Sine Wave Estimation
Frequency Analysis
Machine Learning
Signal Processing

Neural network estimating sine wave frequency

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Estimating the frequency of a sine wave is a valid neural-network task, but it is worth stating the obvious first: if the data is clean and the problem is strictly sinusoidal, classic signal-processing methods such as FFT are usually simpler and more interpretable. A neural network becomes interesting when the signal is noisy, partially observed, mixed with other patterns, or part of a larger learned system.

Frame It as Supervised Regression

The usual setup is:

  • input: sampled waveform values
  • target: the sine-wave frequency

For a simple synthetic dataset, generate many sine waves with random frequency, phase, amplitude, and noise:

python
1import numpy as np
2
3
4def generate_example(num_points=256, sample_rate=256):
5    t = np.arange(num_points) / sample_rate
6    freq = np.random.uniform(1.0, 40.0)
7    phase = np.random.uniform(0, 2 * np.pi)
8    amplitude = np.random.uniform(0.5, 1.5)
9    noise = np.random.normal(scale=0.05, size=num_points)
10
11    signal = amplitude * np.sin(2 * np.pi * freq * t + phase) + noise
12    return signal.astype(np.float32), np.float32(freq)
13
14
15x, y = generate_example()
16print(x.shape, y)

This kind of synthetic generator is useful because frequency labels are exact and cheap to produce.

A Small Keras Model

A 1D convolutional model is a reasonable baseline because the input is a short sequence.

python
1import tensorflow as tf
2import numpy as np
3
4
5X = []
6Y = []
7for _ in range(2000):
8    signal, freq = generate_example()
9    X.append(signal)
10    Y.append(freq)
11
12X = np.array(X)[..., np.newaxis]
13Y = np.array(Y)
14
15model = tf.keras.Sequential([
16    tf.keras.layers.Input(shape=(256, 1)),
17    tf.keras.layers.Conv1D(16, 5, activation="relu"),
18    tf.keras.layers.MaxPooling1D(2),
19    tf.keras.layers.Conv1D(32, 5, activation="relu"),
20    tf.keras.layers.GlobalAveragePooling1D(),
21    tf.keras.layers.Dense(32, activation="relu"),
22    tf.keras.layers.Dense(1),
23])
24
25model.compile(optimizer="adam", loss="mse", metrics=["mae"])
26model.fit(X, Y, epochs=5, batch_size=32, validation_split=0.2)

The network outputs one number: the predicted frequency.

Sampling Assumptions Matter More Than Architecture

A neural network cannot recover frequency information that was not present in the samples. That means:

  • sample rate limits the highest resolvable frequency
  • sequence length affects frequency resolution
  • heavy noise can blur periodic structure

If the waveform is undersampled, the model is learning from aliased data, not from the original sine frequency. That is a data problem, not a deep-learning problem.

In practice, before tuning the network, confirm that the input window is long enough and sampled fast enough for the target frequency range.

Normalize the Problem When Possible

The model learns more easily when amplitude and offset variation are controlled. For example, centering and scaling each waveform can reduce nuisance variation:

python
1def normalize_signal(signal):
2    signal = signal - np.mean(signal)
3    std = np.std(signal)
4    return signal / std if std > 0 else signal

If absolute amplitude is not part of the target, normalization usually helps the model focus on periodicity instead.

Regression Versus Classification

There are two common formulations:

  • regression, where the model predicts a continuous frequency
  • classification, where the model chooses one frequency bin from a fixed set

Regression is more flexible. Classification can be easier to train if the task naturally fits discrete bins, such as identifying one known tone among many predefined choices.

So the right formulation depends on what "frequency estimation" means in the actual system.

Use a Strong Baseline

Before trusting the network, compare it to a classical baseline. Even a simple FFT peak estimate can be a useful benchmark. If the neural network performs worse on clean synthetic data, the problem is probably not the impossibility of the task. It is more likely a data, architecture, or training issue.

That comparison keeps the project honest. Neural networks should beat baselines only when the data really justifies them.

Common Pitfalls

  • Using a neural network when a simple FFT baseline already solves the problem.
  • Ignoring sample rate and sequence length limits.
  • Training only on clean sinusoids and expecting robustness on noisy real signals.
  • Confusing continuous regression with classification over fixed frequency bins.
  • Evaluating the model without comparing it to a non-neural baseline.

Summary

  • Frequency estimation from sine waves can be treated as a supervised learning problem.
  • A small 1D convolutional model is a reasonable starting point.
  • Sampling rate, sequence length, and noise level matter as much as the model choice.
  • Normalize nuisance variation when amplitude is not part of the target.
  • Always compare the network to a classical signal-processing baseline.

Course illustration
Course illustration

All Rights Reserved.