# Untitled

a guest
Jul 28th, 2017
428
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import numpy as np
2. import matplotlib.pyplot as plt
3.
4. """generate signal"""
5. oversampling_factor = 5.0  # when initial Fs = 1600 - interpolate with (1600 * oversampling_factor)
6. Fs = 1000.0  # sampling frequency
7. n = 500.0
8. x = np.arange(n)
9. y_time = [np.random.randn() for item in range(0, int(n))]  # create white noise
10.
11. """interpolate"""
12. x_interpolated = np.arange(0.5, n, 1.0)  # only shifted
13. y_interpolated1 = np.interp(x_interpolated, x, y_time)
14. x_interpolated2 = np.arange(0.0, n, 1.0/oversampling_factor)  # oversampled
15. y_interpolated2 = np.interp(x_interpolated2, x, y_time)
16.
17. "FFT"
18. y_freq = np.abs(np.fft.rfft(y_time)) / len(y_time)  # original signal
19. y_freq_shifted = np.abs(np.fft.rfft(y_interpolated1)) / len(y_interpolated1)  # shifted signal
20. y_freq_oversampled = np.abs(np.fft.rfft(y_interpolated2)) / len(y_interpolated2)  # oversampled signal
21.
22. x_freq = np.fft.rfftfreq(len(y_time), d=(1.0 / Fs))
23. x_freq_shifted = np.fft.rfftfreq(len(y_interpolated1), d=(1.0 / Fs))
24. x_freq_oversampled = np.fft.rfftfreq(len(y_interpolated2), d=(1.0 / (Fs * oversampling_factor)))  # original signal
25.
26. """plot the results"""
27. plt.subplot(211)
28. plt.plot(x, y_time)
29. plt.xlim([0, n])
30. plt.xlabel('sample')
31. plt.subplot(212)
32. plt.plot(x_freq, y_freq, label='original')
33. plt.plot(x_freq_shifted, y_freq_shifted, label='shifted')
34. plt.plot(x_freq_oversampled, y_freq_oversampled, label='oversampled')
35. plt.legend()
36. plt.xlim([0, Fs/2])
37. plt.xlabel('frequency [Hz]')
38. plt.show()
RAW Paste Data