Chacotay

Untitled

Apr 13th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Apr 11 21:30:24 2016
  4.  
  5. @author: Yoshi
  6. """
  7.  
  8. import numpy as np
  9. from numpy import pi
  10. from scipy import stats
  11. import matplotlib.pyplot as plt
  12. import os
  13.  
  14. plt.ioff()
  15. np.random.seed(99)
  16.  
  17. fs = 200                                # Sample frequency (in Hz)
  18. dt = 1. / fs                            # Sample period (s)
  19. maxtime = 2                             # Maximum time in seconds
  20. N = fs * maxtime                        # Total number of samples
  21. t = np.linspace(0, maxtime - dt, N)     # Time array
  22.        
  23. # Sine wave attributes: frequencies (Hz) and amplitudes
  24. f1 = 6      ;       A1 = 1
  25. f2 = 5      ;       A2 = 1
  26. f3 = 25     ;       A3 = 1
  27.  
  28. sin1 = A1*np.sin(2*pi*f1*t)
  29. sin2 = A2*np.sin(2*pi*f2*t)
  30. sin3 = A3*np.sin(2*pi*f3*t)
  31.  
  32. sin_total = sin1
  33.  
  34. # Code from Notes
  35. A = sin_total                           # Input signal
  36. B = np.fft.fft(A)                       # FFT of signal
  37. C = np.zeros(N, dtype='complex128')     # Output array initalization
  38.  
  39. # Hilbert Transform
  40. C[0] = B[0]                     # Hard-code start and
  41. C[N/2] = B[N/2]                 # Nyquist of data output
  42.  
  43. for i in range(1, N/2):         # Double positive frequencies
  44.     C[i] = 2.0 * B[i]
  45.  
  46. for i in range(N/2 + 1, N):     # Zero negative frequencies
  47.     C[i] = 0.0
  48.    
  49. B = np.fft.ifft(C)              # Inverse FFT of C: Store in B
  50.  
  51. # Plot Function and Envelope Function
  52. fig = plt.figure()
  53. fig.text(0.1, -0.01, r'$f_1 = %dHz$, $f_2 = %dHz$' % (f1, f2), fontsize=16)
  54.  
  55. ax = fig.add_subplot(1,1,1)
  56.  
  57. ax.plot(t, A, 'b')
  58. ax.plot(t, abs(B), 'r', lw=2)
  59. ax.set_title('Signal Envelope of Two Closely Spaced Sinusoids')
  60. ax.set_xlabel('Time (s)')
  61. ax.set_ylabel('Amplitude')
  62.  
  63. # Look at Phase Information
  64. phase = np.angle(B)              # Used because np.arctan won't work with ASTC
  65. unwrapped = np.unwrap(phase)     # Write own version of this
Advertisement
Add Comment
Please, Sign In to add comment