def plot_dft_abs_phase(signal: np.array.__class__, frequency_discretization=1, is_in_db=False): spectrum = fftshift(fft(signal)) fft_frequencies = fftshift( fftfreq(signal.shape[0], 1 / frequency_discretization)) abs_values = np.abs(spectrum) if is_in_db: abs_values = power_samples_in_db(abs_values) plt.figure() plt.subplot(211) plt.plot(fft_frequencies, abs_values, color='b') plt.ylabel('Amplitude [dB]', color='b') plt.xlabel('Frequency') plt.grid() plt.subplot(212) plt.plot(fft_frequencies, np.angle(spectrum), color='g') plt.ylabel('Phase [rad]', color='g') plt.xlabel('Frequency') plt.grid() plt.show()
def test_util_filterResponse(self): sr = self.stream[0].stats.sampling_rate # test filterResponse vs filter2 st = self.stream.copy() data = st[0].data.copy() N = len(data) nfft = nextpow2(len(data)) values = util.main.filterResp(1, 5, corners=2, sr=20, N=nfft, whole=True)[1] st.filter2(1, 5, corners=2) data2 = ifft(fft(data, nfft) * values, nfft) np.testing.assert_array_almost_equal(st[0].data, data2[:N]) # test stream interface st = self.stream.copy() st.fft() st.filter2(1, 5, corners=2) st.ifft() np.testing.assert_array_almost_equal(st[0].data, data2[:N]) # filtering with filterResponse and zerophase=Trueproduces a peak at # the end of data. With nfft=N there is no peak anymore, but still the values are not the same st = self.stream.copy() freqs, values = util.main.filterResp(1, 5, sr=20, N=nfft, corners=2, whole=True, zerophase=True) st.filter2(1, 5, corners=2, zerophase=True) data2 = ifft(fft(data, nfft) * values, nfft) np.testing.assert_array_almost_equal(st[0].data[:-10 * sr], data2[:N - 10 * sr]) return from numpy.fft.helper import ifftshift, fftfreq import matplotlib.pyplot as plt real_freqs = (ifftshift(freqs) - np.pi) * 0.5 * sr / np.pi freqs2 = fftfreq(nfft, 1 / 20.) print real_freqs print freqs2 plt.subplot(411) plt.plot(real_freqs, np.abs(values)) ax = plt.subplot(412) plt.plot(data, label='data') plt.legend() plt.subplot(413, sharex=ax) plt.plot(st[0].data, alpha=0.5, label='stream.filter2') plt.plot(data2[:N], alpha=0.5, label='filterResponse') plt.legend() plt.show()
def plot_dft_real_image(signal: np.array.__class__, frequency_discretization=1, is_in_db=False): spectrum = fftshift(fft(signal)) if is_in_db: spectrum = power_samples_in_db( np.real(spectrum)) + 1j * power_samples_in_db(np.imag(spectrum)) fft_frequencies = fftshift( fftfreq(signal.shape[0], 1 / frequency_discretization)) plt.subplot(211) plt.plot(fft_frequencies, np.real(spectrum)) plt.subplot(212) plt.plot(fft_frequencies, np.imag(spectrum)) # plt.ion() plt.show()
def analyzeData(youts, aMags, rMags, y0, p, m): ''' Analyzes data which was saved to CSV output. ''' # Find frequencies between 0.2 and 3 Hz freqs = fftfreq(len(tspan), tspan[1] - tspan[0]) f = array(freqs) idx = logical_and(f > 0.2, f < 3) # Logical index # Find best responses where the frequencies are high between range above sums = [sum(aMags[i, idx]) + sum(rMags[i, idx]) for i in range(0, size(aMags, 0))] sums = array(sums) extract = array(sorted(enumerate(sums), key = lambda s: s[1], reverse = True)) best100 = [int(i) for i in extract[0:100, 0]] stats = calcStats(p[best100]) for i in range(0, len(m.pnames)): print m.pnames[i] + ':', stats['lower95'][i], '|', stats['upper95'][i], '|', stats['median'][i] '''
from numpy.fft.helper import fftfreq, fftshift from scipy.fftpack import ifft, ifftshift from scipy.signal.signaltools import convolve from WiSim.plot_utils import plot_dft_real_image, plot_dft_abs_phase from WiSim.signal_source import SignalSource from WiSim.utils import complex_randn, db_to_power # Construct signal frequency_discretization = 600 * 10 ** 6 period_discretization = 1 / frequency_discretization window_length = int(1024/2) fft_frequencies = fftshift(fftfreq(window_length, period_discretization)) signal_borders = (70 * 10 ** 6, 230 * 10 ** 6) signal_positions = (fft_frequencies > signal_borders[0]) & (fft_frequencies < signal_borders[1]) n_signal_positions = sum(signal_positions) random_component = complex_randn((n_signal_positions,)) coefficients = np.arange(1, random_component.shape[0] + 1) random_component *= coefficients signal = np.zeros((window_length,), dtype=np.complex128) signal[signal_positions] = random_component signal = ifft(ifftshift(signal)) # Adding noise to the signal
def fftfreq(self): if not self.stats.is_fft: raise ValueError('Trace is no fft.') return fftfreq(self.stats.npts, 1. / self.stats.sampling_rate)