0
votes

I try to generate a sine wave signal with a frequency changing over time. The frequency is randomly defined by generating some random values in the range [0.5, 2] and interpolating the points between them.

The expected output signal is a sine wave with a non-changing amplitude and a changing frequency. But there are some smaller bumps and the signal is not a 'smooth' sine wave. E.g. the period at x = 200 should be larger than the period at x = 10, but the opposite is the case.

Does anyone know, what happened here?

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

x_samples = np.arange(-100, 3100, 50)
freq_samples = np.random.random(x_samples.shape) * 1.5 + 0.5

x = np.arange(0, 3000, 0.1)
interpolation = interp1d(x_samples, freq_samples, kind='quadratic')
freq = interpolation(x)
y = np.sin(freq * x)

plt.plot(x, y, label="sin(freq(x) * x)")
plt.plot(x, freq, label="freq(x)")
plt.legend()
plt.show()

enter image description here

1

1 Answers

1
votes

freq * x is probably not doing what's expected. The frequency needs to multiply the change in x for each point not the cumulative x.

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

x_samples = np.arange(-10, 350, 50)
freq_samples = np.random.random(x_samples.shape) * 1.5 + 0.5

x = np.arange(0, 300, 0.1)

dx = np.full_like(x, 0.1 )       # Change in x

interpolation = interp1d(x_samples, freq_samples, kind='quadratic')
freq = interpolation(x)

x_plot = (freq * dx ).cumsum()    # Cumsum freq * change in x

y = np.sin(x_plot)

plt.plot(x, y, label="sin(freq(x) * x)")
plt.plot(x, freq, label="freq(x)")
plt.legend()
plt.show()

Frequency Modulated sine wave.