def test_time_to_freq_to_time(debug=False): print("Test time -> freq -> time") # Create time-domain data t1 = np.arange(0, 20e-3, 1e-4) x1 = _signal_model(t1) # Frequency domain f, X = czt.time2freq(t1, x1) # Back to time domain t2, x2 = czt.freq2time(f, X, t=t1) # Plot for debugging purposes if debug: plt.figure() plt.title("Imaginary") plt.plot(t1, x1.imag, 'k', label='Original') plt.plot(t2, x2.imag, 'r', label='Recovered') plt.legend() plt.figure() plt.title("Real") plt.plot(t1, x1.real, 'k', label='Original') plt.plot(t2, x2.real, 'r', label='Recovered') plt.legend() plt.show() # Compare np.testing.assert_almost_equal(x1, x2, decimal=12)
def test_freq_to_time_convserions(): """Test frequency <-> time domain conversions.""" # Time data t1 = np.arange(0, 20e-3, 1e-4) dt = t1[1] - t1[0] Fs = 1 / dt N = len(t1) # Signal data def model(t): output = (1.0 * np.sin(2 * np.pi * 1e3 * t) + 0.3 * np.sin(2 * np.pi * 2e3 * t) + 0.1 * np.sin(2 * np.pi * 3e3 * t)) * np.exp(-1e3 * t) return output x1 = model(t1) # Frequency domain f, X = czt.time2freq(t1, x1) # Back to time domain t2, x2 = czt.freq2time(f, X) # # Debug # import matplotlib.pyplot as plt # plt.figure() # plt.plot(t1, x1.real) # plt.plot(t2, x2.real) # plt.show() # Compare np.testing.assert_almost_equal(x1, x2, decimal=12)
def test_time_zoom(debug=False): print("Test time-domain zoom") # Create time-domain data t = np.arange(0, 20e-3 + 1e-10, 1e-4) x = _signal_model(t) dt = t[1] - t[0] # Generate frequency-domain signal using DFT f, X = czt.dft(t, x) # Time domain t1, x1 = czt.freq2time(f, X, t=t) # Time domain: zoom mask = (0.001 <= t1) & (t1 <= 0.002) t2, x2 = czt.freq2time(f, X, t=t1[mask]) # Plot for debugging purposes if debug: plt.figure() plt.title("Imaginary") plt.plot(t, np.imag(x), 'k-', label='Original') plt.plot(t1, np.imag(x1), 'ro:', label='freq2time: full') plt.plot(t2, np.imag(x2), 'bv-', label='freq2time: zoom') plt.xlim([0, 0.003]) plt.legend() plt.figure() plt.title("Real") plt.plot(t, np.real(x), 'k-', label='Original') plt.plot(t1, np.real(x1), 'ro:', label='freq2time: full') plt.plot(t2, np.real(x2), 'bv-', label='freq2time: zoom') plt.xlim([0, 0.003]) plt.legend() plt.show() # Compare np.testing.assert_almost_equal(x, x1, decimal=12) np.testing.assert_almost_equal(x[mask], x2, decimal=12)
def test_compare_czt_to_analytic_expression(debug=False): print("Compare CZT to analytic expression") # Create time-domain data t = np.linspace(0, 50, 10001) * 1e-3 x = _signal_model(t) # CZT f, X_czt = czt.time2freq(t, x) # Build frequency domain signal X = _signal_model_f(f, len(t)) # Transform back to time-domain _, x_iczt = czt.freq2time(f, X_czt, t=t) # Truncate mask = (0 < f) & (f < 5e3) f, X, X_czt = f[mask], X[mask], X_czt[mask] # Plot for debugging purposes if debug: plt.figure() plt.title("Freq-Domain: Imaginary") plt.plot(f / 1e3, X_czt.imag, label="CZT") plt.plot(f / 1e3, X.imag, 'r--', label="Analytic") plt.legend() plt.figure() plt.title("Freq-Domain: Real") plt.plot(f / 1e3, X_czt.real, label="CZT") plt.plot(f / 1e3, X.real, 'r--', label="Analytic") plt.legend() plt.figure() plt.title("Freq-Domain: Absolute") plt.plot(f / 1e3, np.abs(X_czt), label="CZT") plt.plot(f / 1e3, np.abs(X), 'r--', label="Analytic") plt.legend() plt.show() # Compare np.testing.assert_allclose(X, X_czt, atol=0.1) np.testing.assert_almost_equal(x, x_iczt, decimal=12)
def test_compare_iczt_idft(debug=False): print("Compare ICZT and IDFT") # Create time-domain signal t = np.arange(0, 20e-3, 1e-4) x = _signal_model(t) # Frequency domain using DFT f, X = czt.dft(t, x) # Get time-domain using ICZT _, x_iczt = czt.freq2time(f, X, t) # Get time-domain using IDFT _, x_idft = czt.idft(f, X, t) # Plot for debugging purposes if debug: plt.figure() plt.title("Imaginary") plt.plot(t, x.imag, 'k', label="Original") plt.plot(t, x_iczt.imag, 'g:', label="ICZT") plt.plot(t, x_idft.imag, 'r--', label="IDFT") plt.legend() plt.figure() plt.title("Real") plt.plot(t, x.real, 'k', label="Original") plt.plot(t, x_iczt.real, 'g:', label="ICZT") plt.plot(t, x_idft.real, 'r--', label="IDFT") plt.legend() plt.figure() plt.title("Real: error") plt.plot(t, x_iczt.real - x.real, 'k', label="Original") plt.show() # Compare np.testing.assert_almost_equal(x_iczt, x, decimal=12) np.testing.assert_almost_equal(x_idft, x, decimal=12) np.testing.assert_almost_equal(x_iczt, x_idft, decimal=12)