def add_noise(snd_array,fps): noise1 = noise.white(len(snd_array)) noise1 = map(lambda x: int(x/.01), noise1) noise2 = np.array([noise1,noise1]).T noisy_array = noise2 + snd_array filename = "noisy_test.wav" scaled = np.int16(noisy_array/np.max(np.abs(noisy_array)) * 32767) write(filename, 44100, scaled) return
def many_psds(k=2, fs=1.0, b0=1.0, N=1024): """ compute average of many PSDs """ psd = [] for j in range(k): print(j) x = noise.white(num_points=2 * 4096, b0=b0, fs=fs) f, tmp = noise.numpy_psd(x, fs) if j == 0: psd = tmp else: psd = psd + tmp return f, psd / k
def many_psds(k=2,fs=1.0, b0=1.0, N=1024): """ compute average of many PSDs """ psd=[] for j in range(k): print j x = noise.white(N=2*4096,b0=b0,fs=fs) f, tmp = noise.numpy_psd(x,fs) if j==0: psd = tmp else: psd = psd + tmp return f, psd/k
def add_noise(snd_array, fps): noise1 = noise.white(len(snd_array)) noise1 = map(lambda x: int(x / .01), noise1) noise2 = np.array([noise1, noise1]).T noisy_array = noise2 + snd_array filename = "noisy_test.wav" scaled = np.int16(noisy_array / np.max(np.abs(noisy_array)) * 32767) write(filename, 44100, scaled) return
def test_noise(): N = 500 rate = 1.0 w = noise.white(N) b = noise.brown(N) v = noise.violet(N) p = noise.pink(N) assert len(w) == N assert len(b) == N assert len(v) == N - 1 # why? assert len(p) == N
def test_noise(): N = 500 rate = 1.0 w = noise.white(N) b = noise.brown(N) v = noise.violet(N) p = noise.pink(N) assert len(w) == N assert len(b) == N assert len(v) == N-1 # why? assert len(p) == N
def add_violet_noise(filename): print "Adding Violet Noise..." fps, snd_array = wavfile.read('../audio/transformed/' + filename) noise1 = noise.white(len(snd_array)) noise1 = map(lambda x: int(x/.01), noise1) noise2 = np.array([noise1,noise1]).T noisy_array = noise2 + snd_array filename, file_extension = os.path.splitext(filename) filename = "../audio/transformed/"+filename+"violetnoise.wav" scaled = np.int16(noisy_array/np.max(np.abs(noisy_array)) * 32767) write(filename, 44100, scaled) return
def test_noise(): N = 500 #rate = 1.0 w = noise.white(N) b = noise.brown(N) v = noise.violet(N) p = noise.pink(N) # check output length assert len(w) == N assert len(b) == N assert len(v) == N assert len(p) == N # check output type for x in [w, b, v, p]: assert type(x) == numpy.ndarray, "%s is not numpy.ndarray" % (type(x))
def test_ns(): # this test asks for results at unreasonable tau-values # either zero, not an integer multiple of the data-interval # or too large, given the length of the dataset N = 500 rate = 1.0 phase_white = noise.white(N) taus_try = [x for x in numpy.logspace(0, 4, 4000)] # try insane tau values _test(allan.adev, phase_white, rate, taus_try) _test(allan.oadev, phase_white, rate, taus_try) _test(allan.mdev, phase_white, rate, taus_try) _test(allan.tdev, phase_white, rate, taus_try) _test(allan.hdev, phase_white, rate, taus_try) _test(allan.ohdev, phase_white, rate, taus_try) _test(allan.totdev, phase_white, rate, taus_try) _test(allan.mtie, phase_white, rate, taus_try) _test(allan.tierms, phase_white, rate, taus_try)
def test_ns(): # this test asks for results at unreasonable tau-values # either zero, not an integer multiple of the data-interval # or too large, given the length of the dataset N = 500 rate = 1.0 phase_white = noise.white(N) taus_try = [x for x in numpy.logspace(0,4,4000)] # try insane tau values _test( allan.adev, phase_white, rate, taus_try) _test( allan.oadev, phase_white, rate, taus_try) _test( allan.mdev, phase_white, rate, taus_try) _test( allan.tdev, phase_white, rate, taus_try) _test( allan.hdev, phase_white, rate, taus_try) _test( allan.ohdev, phase_white, rate, taus_try) _test( allan.totdev, phase_white, rate, taus_try) _test( allan.mtie, phase_white, rate, taus_try) _test( allan.tierms, phase_white, rate, taus_try)
# the predicted S_y, S_fi, S_x, and ADEV given in the table # AW 2015-07-29 # from the ieee1139 table # PSD_y(f) = h2 * f^2 fractional frequency PSD # PSD_fi(f) = h2 * v0^2 * f^0 phase (radians) PSD # PSD_x(f) = h2 * (2 pi )^-2 phase (time) PSD # ADEV_y(tau) = sqrt{ 3*fh / (4*pi^2) * h2 * tau^-2 } Allan deviation # fh is the upper limit for the noise process, otherwise we would have infinite power... fs=10e6 h2=1e-26 N=32*4096 v0 = 10e6 # nominal oscillator frequency fi = noise.white(N=N,b0=h2*v0*v0,fs=fs) # phase in radians x = [fifi/(2*math.pi*v0) for fifi in fi] # phase in seconds y = (fs)*np.diff(x) # fractional frequency t = np.linspace(0, (1.0/fs)*N, len(y)) plt.figure() plt.subplot(2,2,1) plt.plot(t,[1e6*yy for yy in y],'b') plt.xlabel('Time / s') plt.ylabel('Fractional frequency / PPM') plt.title('Fractional frequency time-series') plt.subplot(2,2,2) plt.plot(t,[1e9*tt for tt in x[1:]],'r') plt.xlabel('Time / s') plt.ylabel('Phase / ns')
def test_dataset_parameters(): ds = Dataset() ds.set_input(noise.white(10), rate=1.234, data_type="frequency", taus=[1, 3, 4])
def test_psd2allan_figure(): f = np.arange( 1e4 + 1 ) # generate f-vector 0...10^4 Hz in 1 Hz steps -> Nyquist freq is 5 kHz S_y0 = 1e-24 # some arbitrarily chosen noise level S_y_WFM = S_y0 * np.ones( np.size(f)) # generate white frequency noise S_y(f) S_y_WPM = S_y_WFM * f**2 # white phase noise S_y(f) plt.rc('text', usetex=True) plt.close('all') plt.figure(1) plt.loglog(f[f > 0], S_y_WFM[f > 0]) plt.loglog(f[f > 0], S_y_WPM[f > 0]) y_WFM_ind = noise.white(num_points=int(2e4), b0=S_y0, fs=2e4) y_WPM_ind = noise.violet(num_points=int(2e4), b2=S_y0, fs=2e4) f, S_y_WFM_ind = welch(y_WFM_ind, fs=2e4, nperseg=y_WFM_ind.size, window='hanning') f, S_y_WPM_ind = welch(y_WPM_ind, fs=2e4, nperseg=y_WPM_ind.size, window='hanning') plt.loglog(f, S_y_WFM_ind) plt.loglog(f, S_y_WPM_ind) plt.xlabel('$f$ [Hz]') plt.ylabel('$S_y$') plt.legend(('WFM direct', 'WPM direct', 'WFM indirect', 'WPM indirect')) #(tau, sigma_WFM)= at.psd2allan(S_y_WFM, f, kind= 'a') #(tau, sigma_WPM)= at.psd2allan(S_y_WPM, f, kind= 'a') #(tau, modsigma_WFM)= at.psd2allan(S_y_WFM, f, kind= 'm') #(tau, modsigma_WPM)= at.psd2allan(S_y_WPM, f, kind= 'm') (tau, sigma_WFM) = at.psd2allan(S_y_WFM, kind='a') (tau, sigma_WPM) = at.psd2allan(S_y_WPM, kind='a') (tau, modsigma_WFM) = at.psd2allan(S_y_WFM, kind='m') (tau, modsigma_WPM) = at.psd2allan(S_y_WPM, kind='m') plt.figure(2) plt.loglog(tau, sigma_WFM) plt.loglog(tau, sigma_WPM) plt.loglog(tau, modsigma_WFM) plt.loglog(tau, modsigma_WPM) (tau, sigma_WFM_ind) = at.psd2allan(S_y_WFM_ind, kind='a') (tau, sigma_WPM_ind) = at.psd2allan(S_y_WPM_ind, kind='a') (tau, modsigma_WFM_ind) = at.psd2allan(S_y_WFM_ind, kind='m') (tau, modsigma_WPM_ind) = at.psd2allan(S_y_WPM_ind, kind='m') plt.loglog(tau, sigma_WFM_ind, ':C0') plt.loglog(tau, sigma_WPM_ind, ':C1') plt.loglog(tau, modsigma_WFM_ind, ':C2') plt.loglog(tau, modsigma_WPM_ind, ':C3') f_h = 1e4 sigma_WFM_theo = np.sqrt(S_y0 / tau / 2.0) sigma_WPM_theo = np.sqrt(3.0 * f_h * S_y0 / tau**2 / (2.0 * np.pi)**2) modsigma_WFM_theo = np.sqrt(S_y0 / tau / 4.0) modsigma_WPM_theo = np.sqrt(3.0 * S_y0 / tau**3 / (2.0 * np.pi)**2 / 2) plt.loglog(tau, sigma_WFM_theo, '.C0') plt.loglog(tau, sigma_WPM_theo, '.C1') plt.loglog(tau, modsigma_WFM_theo, '.C2') plt.loglog(tau, modsigma_WPM_theo, '.C3') plt.xlabel(r'$\tau$ [s]') plt.ylabel(r'$\sigma_y$') plt.legend( ('psd2allan direct, ADEV, WFM', 'psd2allan direct, ADEV, WPM', 'psd2allan direct, modADEV, WFM', 'psd2allan direct, modADEV, WPM', 'psd2allan indirect, ADEV, WFM', 'psd2allan indirect, ADEV, WPM', 'psd2allan indirect, modADEV, WFM', 'psd2allan indirect, modADEV, WPM', 'theoretical, ADEV, WFM', 'theoretical, ADEV, WPM', 'theoretical, modADEV, WFM', 'theoretical, modADEV, WPM'))
# AW 2015-07-29 # from the ieee1139 table # PSD_y(f) = h2 * f^2 fractional frequency PSD # PSD_fi(f) = h2 * v0^2 * f^0 phase (radians) PSD # PSD_x(f) = h2 * (2 pi )^-2 phase (time) PSD # ADEV_y(tau) = sqrt{ 3*fh / (4*pi^2) * h2 * tau^-2 } Allan deviation # fh is the upper limit for the noise process, # otherwise we would have infinite power... fs = 10e6 h2 = 1e-26 N = 32 * 4096 v0 = 10e6 # nominal oscillator frequency fi = noise.white(num_points=N, b0=h2 * v0 * v0, fs=fs) # phase in radians x = [fifi / (2 * math.pi * v0) for fifi in fi] # phase in seconds y = (fs) * np.diff(x) # fractional frequency t = np.linspace(0, (1.0 / fs) * N, len(y)) plt.figure() plt.subplot(2, 2, 1) plt.plot(t, [1e6 * yy for yy in y], 'b') plt.xlabel('Time / s') plt.ylabel('Fractional frequency / PPM') plt.title('Fractional frequency time-series') plt.subplot(2, 2, 2) plt.plot(t, [1e9 * tt for tt in x[1:]], 'r') plt.xlabel('Time / s') plt.ylabel('Phase / ns')
def dataset(): return Dataset(noise.white(10))
psd = [] for j in range(k): print j x = noise.white(N=2 * 4096, b0=b0, fs=fs) f, tmp = noise.numpy_psd(x, fs) if j == 0: psd = tmp else: psd = psd + tmp return f, psd / k fs = 256 # sample rate b0 = 0.0002 N = 2 * 4096 x = noise.white(N=2 * 4096, b0=b0, fs=fs) t = np.linspace(0, (1.0 / fs) * N, len(x)) plt.figure() plt.plot(t, x) plt.xlabel('Time / s') plt.ylabel('Amplitude / V') print x f, psd = many_psds(k=50, fs=fs, b0=b0, N=N) fxx, Pxx_den = noise.scipy_psd(x, fs) plt.figure() plt.semilogy(f, psd, label='numpy.fft()') plt.semilogy(fxx, Pxx_den, label='scipy.signal.welch()') plt.semilogy(f, [b0] * len(f), label='b_0 = %.3g' % b0)
phase_rw_rw = numpy.cumsum(noise.brown(N)) # integrate to get phase plotallan(plt, freq_rw, 1, t, 'm.') plotallan_phase(plt, phase_rw_rw, 1, t, 'mo',label='random walk frequency') plotline(plt, +0.5, t, 'm',label="f^(+1/2)") # pink frequency noise => constant ADEV print("Pink frequency noise - should have constant ADEV") freq_pink = noise.pink(N) phase_p = numpy.cumsum(noise.pink(N)) # integrate to get phase, color?? plotallan_phase(plt, phase_p, 1, t, 'co',label="pink/flicker frequency noise") plotallan(plt, freq_pink, 1, t, 'c.') plotline(plt, 0, t, 'c',label="f^0") # white frequency modulation => 1/sqrt(tau) ADEV print("White frequency noise - should have 1/sqrt(tau) ADEV") freq_white = noise.white(N) phase_rw = noise.brown(N) # integrate to get Brownian, or random walk phase plotallan(plt, freq_white, 1, t, 'b.') plotallan_phase(plt, phase_rw, 1, t, 'bo',label='random walk phase a.k.a. white frequency noise') plotline(plt, -0.5, t, 'b',label="f^(-1/2)") # pink phase noise => 1/tau ADEV and MDEV print("Pink phase noise - should tau^(-3/2) MDEV") phase_pink = noise.pink(N) plotallan_phase(plt, phase_pink, 1, t, 'ko',label="pink/flicker phase noise") plotline(plt, -1, t, 'k',label="f^(-1)") # white phase noise => 1/tau ADEV and tau^(-3/2) MDEV print("White phase noise - should have 1/tau ADEV") phase_white = noise.white(N) plotallan_phase(plt, phase_white, 1, t, 'ro',label="white phase noise")
# the predicted S_y, S_fi, S_x, and ADEV given in the table # AW 2015-07-29 # from the ieee1139 table # PSD_y(f) = h2 * f^2 fractional frequency PSD # PSD_fi(f) = h2 * v0^2 * f^0 phase (radians) PSD # PSD_x(f) = h2 * (2 pi )^-2 phase (time) PSD # ADEV_y(tau) = sqrt{ 3*fh / (4*pi^2) * h2 * tau^-2 } Allan deviation # fh is the upper limit for the noise process, otherwise we would have infinite power... fs=10e6 h2=1e-26 N=32*4096 v0 = 10e6 # nominal oscillator frequency fi = noise.white(num_points=N,b0=h2*v0*v0,fs=fs) # phase in radians x = [fifi/(2*math.pi*v0) for fifi in fi] # phase in seconds y = (fs)*np.diff(x) # fractional frequency t = np.linspace(0, (1.0/fs)*N, len(y)) plt.figure() plt.subplot(2,2,1) plt.plot(t,[1e6*yy for yy in y],'b') plt.xlabel('Time / s') plt.ylabel('Fractional frequency / PPM') plt.title('Fractional frequency time-series') plt.subplot(2,2,2) plt.plot(t,[1e9*tt for tt in x[1:]],'r') plt.xlabel('Time / s') plt.ylabel('Phase / ns')
# the predicted S_y, S_fi, S_x, and ADEV given in the table # AW 2015-07-29 # from the ieee1139 table # PSD_y(f) = h0 * f^0 fractional frequency PSD # PSD_fi(f) = h0 * vo^2 * f^-2 phase (radians) PSD # PSD_x(f) = h0 * (2 pi f)^-2 phase (time) PSD # ADEV_y(tau) = sqrt{ 1/2 * h0 * tau^-1 } Allan deviation fs=100 h0=2e-16 N=16*4096 v0 = 1e6 # nominal oscillator frequency y = noise.white(N=N,b0=h0,fs=fs) # fractional frequency x = allantools.frequency2phase(y,fs) # phase in seconds fi = [2*math.pi*v0*xx for xx in x] # phase in radians t = np.linspace(0, (1.0/fs)*N, len(y)) plt.figure() plt.plot(t,y) plt.xlabel('Time / s') plt.ylabel('Fractional frequency') f_y, psd_y = noise.numpy_psd(y,fs) f_fi, psd_fi = noise.numpy_psd(fi,fs) f_x, psd_x = noise.numpy_psd(x,fs) fxx, Pxx_den = noise.scipy_psd(y, fs) f_fi2, psd_fi2 = noise.scipy_psd(fi, fs) f_x2, psd_x2 = noise.scipy_psd(x, fs)
psd = [] for j in range(k): print(j) x = noise.white(num_points=2 * 4096, b0=b0, fs=fs) f, tmp = noise.numpy_psd(x, fs) if j == 0: psd = tmp else: psd = psd + tmp return f, psd / k fs = 256 # sample rate b0 = 0.0002 N = 2 * 4096 x = noise.white(num_points=2 * 4096, b0=b0, fs=fs) t = np.linspace(0, (1.0 / fs) * N, len(x)) plt.figure() plt.plot(t, x) plt.xlabel('Time / s') plt.ylabel('Amplitude / V') print(x) f, psd = many_psds(k=50, fs=fs, b0=b0, N=N) fxx, Pxx_den = noise.scipy_psd(x, fs) plt.figure() plt.semilogy(f, psd, label='numpy.fft()') plt.semilogy(fxx, Pxx_den, label='scipy.signal.welch()') plt.semilogy(f, [b0] * len(f), label='b_0 = %.3g' % b0)
psd=[] for j in range(k): print j x = noise.white(N=2*4096,b0=b0,fs=fs) f, tmp = noise.numpy_psd(x,fs) if j==0: psd = tmp else: psd = psd + tmp return f, psd/k fs=256 # sample rate b0=0.0002 N=2*4096 x = noise.white(N=2*4096,b0=b0,fs=fs) t = np.linspace(0, (1.0/fs)*N, len(x)) plt.figure() plt.plot(t,x) plt.xlabel('Time / s') plt.ylabel('Amplitude / V') print x f, psd = many_psds(k=50,fs=fs,b0=b0,N=N) fxx, Pxx_den = noise.scipy_psd(x, fs) plt.figure() plt.semilogy(f,psd,label='numpy.fft()') plt.semilogy(fxx,Pxx_den,label='scipy.signal.welch()') plt.semilogy(f,[b0]*len(f),label='b_0 = %.3g' % b0 )
# the predicted S_y, S_fi, S_x, and ADEV given in the table # AW 2015-07-29 # from the ieee1139 table # PSD_y(f) = h0 * f^0 fractional frequency PSD # PSD_fi(f) = h0 * vo^2 * f^-2 phase (radians) PSD # PSD_x(f) = h0 * (2 pi f)^-2 phase (time) PSD # ADEV_y(tau) = sqrt{ 1/2 * h0 * tau^-1 } Allan deviation fs = 100 h0 = 2e-16 N = 16 * 4096 v0 = 1e6 # nominal oscillator frequency y = noise.white(num_points=N, b0=h0, fs=fs) # fractional frequency x = allantools.frequency2phase(y, fs) # phase in seconds fi = [2 * math.pi * v0 * xx for xx in x] # phase in radians t = np.linspace(0, (1.0 / fs) * N, len(y)) plt.figure() plt.plot(t, y) plt.xlabel("Time / s") plt.ylabel("Fractional frequency") f_y, psd_y = noise.numpy_psd(y, fs) f_fi, psd_fi = noise.numpy_psd(fi, fs) f_x, psd_x = noise.numpy_psd(x, fs) fxx, Pxx_den = noise.scipy_psd(y, fs) f_fi2, psd_fi2 = noise.scipy_psd(fi, fs) f_x2, psd_x2 = noise.scipy_psd(x, fs)
# pink frequency noise => constant ADEV print("Pink frequency noise - should have constant ADEV") freq_pink = noise.pink(N) phase_p = numpy.cumsum(noise.pink(N)) # integrate to get phase, color?? plotallan_phase(plt, phase_p, 1, t, 'co', label="pink/flicker frequency noise") plotallan(plt, freq_pink, 1, t, 'c.') plotline(plt, 0, t, 'c', label="f^0") # white frequency modulation => 1/sqrt(tau) ADEV print("White frequency noise - should have 1/sqrt(tau) ADEV") freq_white = noise.white(N) phase_rw = noise.brown( N) # integrate to get Brownian, or random walk phase plotallan(plt, freq_white, 1, t, 'b.') plotallan_phase(plt, phase_rw, 1, t, 'bo', label='random walk phase a.k.a. white frequency noise') plotline(plt, -0.5, t, 'b', label="f^(-1/2)") # pink phase noise => 1/tau ADEV and MDEV print("Pink phase noise - should tau^(-3/2) MDEV") phase_pink = noise.pink(N) plotallan_phase(plt,
def test_dataset_parameters(): ds = Dataset() ds.set_input( noise.white(10), rate=1.234, data_type = "frequency", taus = [1,3,4] )
# produces synthetic dataset with given PSD and compares against # the predicted S_y, S_fi, S_x, and ADEV given in the table # AW 2015-07-29 # from the ieee1139 table # PSD_y(f) = h0 * f^0 fractional frequency PSD # PSD_fi(f) = h0 * vo^2 * f^-2 phase (radians) PSD # PSD_x(f) = h0 * (2 pi f)^-2 phase (time) PSD # ADEV_y(tau) = sqrt{ 1/2 * h0 * tau^-1 } Allan deviation fs = 100 h0 = 2e-16 N = 16 * 4096 v0 = 1e6 # nominal oscillator frequency y = noise.white(num_points=N, b0=h0, fs=fs) # fractional frequency x = allantools.frequency2phase(y, fs) # phase in seconds fi = [2 * math.pi * v0 * xx for xx in x] # phase in radians t = np.linspace(0, (1.0 / fs) * N, len(y)) plt.figure() plt.plot(t, y) plt.xlabel('Time / s') plt.ylabel('Fractional frequency') f_y, psd_y = noise.numpy_psd(y, fs) f_fi, psd_fi = noise.numpy_psd(fi, fs) f_x, psd_x = noise.numpy_psd(x, fs) fxx, Pxx_den = noise.scipy_psd(y, fs) f_fi2, psd_fi2 = noise.scipy_psd(fi, fs) f_x2, psd_x2 = noise.scipy_psd(x, fs)