Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- """generate signal"""
- oversampling_factor = 5.0 # when initial Fs = 1600 - interpolate with (1600 * oversampling_factor)
- Fs = 1000.0 # sampling frequency
- n = 500.0
- x = np.arange(n)
- y_time = [np.random.randn() for item in range(0, int(n))] # create white noise
- """interpolate"""
- x_interpolated = np.arange(0.5, n, 1.0) # only shifted
- y_interpolated1 = np.interp(x_interpolated, x, y_time)
- x_interpolated2 = np.arange(0.0, n, 1.0/oversampling_factor) # oversampled
- y_interpolated2 = np.interp(x_interpolated2, x, y_time)
- "FFT"
- y_freq = np.abs(np.fft.rfft(y_time)) / len(y_time) # original signal
- y_freq_shifted = np.abs(np.fft.rfft(y_interpolated1)) / len(y_interpolated1) # shifted signal
- y_freq_oversampled = np.abs(np.fft.rfft(y_interpolated2)) / len(y_interpolated2) # oversampled signal
- x_freq = np.fft.rfftfreq(len(y_time), d=(1.0 / Fs))
- x_freq_shifted = np.fft.rfftfreq(len(y_interpolated1), d=(1.0 / Fs))
- x_freq_oversampled = np.fft.rfftfreq(len(y_interpolated2), d=(1.0 / (Fs * oversampling_factor))) # original signal
- """plot the results"""
- plt.subplot(211)
- plt.plot(x, y_time)
- plt.xlim([0, n])
- plt.xlabel('sample')
- plt.subplot(212)
- plt.plot(x_freq, y_freq, label='original')
- plt.plot(x_freq_shifted, y_freq_shifted, label='shifted')
- plt.plot(x_freq_oversampled, y_freq_oversampled, label='oversampled')
- plt.legend()
- plt.xlim([0, Fs/2])
- plt.xlabel('frequency [Hz]')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement