Advertisement
Guest User

python code

a guest
Mar 21st, 2018
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. %matplotlib notebook
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from mpl_toolkits.mplot3d import Axes3D
  5.  
  6. T = 12; #[s] durata
  7. N = 32; # numarul de armonici
  8. Ne = 1000; # numar de esantioana de timp
  9. Te = T/Ne; #[s] perioada de esantionare
  10.  
  11. # Axa timp
  12. t = np.arange(0,T,Te);
  13. t = np.asmatrix(t);
  14.  
  15.  
  16. # Punctele desanate vor fi de coordonate (x,y,z), respectiv (t,n,u)
  17. # unde:
  18. # t - timp (pe x)
  19. # n - armonica corespunzatoare pulsatie n*omega (a n-a pulsatie fundamentala)
  20. # u - semnalul u(t) corespunzator
  21.  
  22. # Coordonata x corespunzatoare timpului
  23. # (sunt necesare N+1 astfel de "axe" - N armonici + 1 suma lor)
  24.  
  25. t = t.transpose();
  26. ones_matrix = np.ones((1, N+1), dtype=np.float);
  27. X = np.dot(t,ones_matrix);
  28.  
  29. # Coordonata y corespunzatoare indicelui armonicii
  30. # (sunt necesare N+1 astfel de "axe" - N armonici + 1 suma lor)
  31.  
  32. n_axa = np.arange(0,N+1,1);
  33. n_axa = np.asmatrix(n_axa);
  34.  
  35. ones_matrix = np.ones((Ne, 1), dtype=np.float);
  36. Y = np.dot(ones_matrix,n_axa);
  37.  
  38. # Coordonata z
  39. # Amplitudinile corespunzatoare
  40. n = np.arange(1,N+1,1);
  41. n = np.asmatrix(n);
  42.  
  43.  
  44. pi_n = np.pi * n;
  45. an =(np.cos((4*pi_n)/5)-1+np.cos((7*pi_n)/5)-np.cos(pi_n)+np.cos(2*pi_n)-np.cos((9*pi_n)/5))/(-pi_n);
  46. bn =(np.sin((4*pi_n)/5)+np.sin((7*pi_n)/5)-np.sin(pi_n)+np.sin(2*pi_n)-np.sin((9*pi_n)/5))/pi_n;
  47.  
  48. ones_matrix = np.ones((Ne, 1), dtype=np.float);
  49. An = np.dot(ones_matrix,an);
  50. Bn = np.dot(ones_matrix,bn);
  51.  
  52.  
  53. # matricea de fecvente ce se aplica  
  54. w = 2*np.pi/T;
  55. Y_part = Y[:,1:(N+1)];
  56. W_n=  np.multiply(w,Y_part);
  57. X_part = X[:,1:(N+1)];
  58. fi = np.multiply(W_n,X_part);
  59.  
  60. Z = X; # initializare
  61. Z[:,1:(N+1)] = np.multiply(An,np.sin(fi)) # + np.multiply(Bn,np.cos(fi));
  62.  
  63. # suma
  64. ones_matrix = np.ones((N,1), dtype=np.float);
  65. Z_part = Z[:,1:(N+1)];
  66. Z[:,0] = np.dot(Z_part,ones_matrix);
  67.  
  68.  
  69. ones_matrix =  np.ones((1,N), dtype=np.float);
  70. one_array = [1, 1];
  71. one_array = np.asmatrix(one_array);
  72. one_array = one_array.transpose();
  73. cx = (T+Te) * one_array * ones_matrix;
  74. one_matrix = [[1] , [1]];
  75. cy = np.dot(one_matrix,n);
  76. zeros_matrix = np.zeros((1,N), dtype=np.float);
  77. abs_an = np.abs(an);
  78. cz = np.concatenate((zeros_matrix,abs_an), axis = 0);
  79.  
  80.  
  81. fig = plt.figure(figsize=(9,10));
  82. ax = fig.add_subplot(111, projection='3d');
  83. ax.plot_wireframe(X[:,1:(N+1)],Y[:,1:(N+1)],Z[:,1:(N+1)], label = '',color="black");
  84. ax.plot_wireframe(X[:,1],Y[:,1],Z[:,1], label = '',color="blue");
  85. ax.legend();
  86. ax.set_xlabel('Timp');
  87. ax.set_ylabel('Indice armonica n ( n*2*pi*f = frecventa)');
  88. ax.set_zlabel('Amplitudine');
  89. ax.grid();
  90. ax.plot_wireframe(cx,cy,cz,color="red");
  91. plt.show();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement