Advertisement
Guest User

Untitled

a guest
Nov 1st, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import scipy.signal as sig
  4.  
  5. #%%## AM signal generation ###################################################
  6. duration = 1 # in seconds
  7. fs = 44100 # Hz
  8. carrier_freq = 2000 # Hz
  9. mod_freq = 200
  10. t = np.linspace(0,duration,endpoint=True,num=fs)
  11. audio = (1 + 1 * np.cos(2*np.pi*mod_freq*t)) * np.cos(2*np.pi*carrier_freq*t)
  12.  
  13. #%%###########################################################################
  14. f, _, Zxx = sig.stft(audio, noverlap=(256 - 32), fs=fs)
  15. aZxx = np.abs(Zxx)
  16.  
  17. #%%## Plot synchrosqueezed CWT ###############################################
  18. plt.imshow(aZxx, aspect='auto', cmap='bone')
  19. plt.show()
  20. #%%## Find carrier freq ######################################################
  21. max_row_idx = np.where(aZxx == aZxx.max())[0]
  22. max_row = aZxx[max_row_idx].squeeze()
  23.  
  24. # print peak's frequency
  25. print(f[max_row_idx])
  26. #%%###########################################################################
  27. # plot amplitude modulator
  28. plt.plot(max_row[:800]); plt.show()
  29.  
  30. # find modulator frequency
  31. max_row_fft = np.abs(np.fft.rfft(max_row))
  32. plt.plot(max_row_fft); plt.show()
  33.  
  34. # peak at 200; exclude dc term
  35. peak_bin = np.argmax(max_row_fft[1:]) + 1
  36. print(peak_bin)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement