Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. def d(n,n0):
  2. """d returns the discrete impulse function evaluated for the values in n (numpy array) with an offset n0.
  3. Inputs :
  4. * n : numpy.array of int, discrete values where the discrete impulse function will be evaluated
  5. * n0 : int, offset of the discrete impulse function
  6.  
  7. Outputs :
  8. * y : numpy.array, discrete values of the discrete impulse signal evaluated for the input n
  9.  
  10. Creation : 14-02-2020
  11. """
  12.  
  13. index_arr = np.where(n==n0)
  14. n = np.zeros(len(n))
  15. for index in index_arr:
  16. n[index] = 1
  17. return n
  18.  
  19. def u(n,n0):
  20. """u returns the step function evaluated for the values in n (numpy array) with an offset n0.
  21. Following the mathematic definitions:
  22. - u(n,n0) = 1 for every i in n such that i>=n0
  23. - u(n,n0) = 0 for every i in n such that i<n0
  24. Inputs :
  25. * n : numpy.array of int, discrete values where the step function will be evaluated
  26. * n0 : int, offset of the step function
  27.  
  28. Outputs :
  29. * y : numpy.array, step function evaluated for n with offset n0
  30.  
  31. Creation : 14-02-2020
  32. """
  33. out = np.empty(len(n))
  34. for i in range(0,len(n)):
  35. if (n[i]<n0):
  36. out[i] = 0
  37. else:
  38. out[i] = 1
  39. return out
  40.  
  41. def r(n,n0):
  42. """r returns the ramp function for the discrete values in n with offset n0
  43. Inputs :
  44. * n : numpy.array of int, discrete values where the ramp function will be evaluated
  45. * n0 : int, offset of the ramp function
  46.  
  47. Outputs :
  48. * y : numpy.array, ramp function evaluated for n with offset n0
  49.  
  50. Creation : 14-02-2020
  51. """
  52. out = np.empty(len(n))
  53. for i in range(0,len(n)):
  54. if(n[i]-n0<=0):
  55. out[i] = 0
  56. else:
  57. out[i] = n[i]-n0
  58. return out
  59.  
  60. def plotFig(y,n,name):
  61. """plotFig stem plot n for x axis and y for y axis and save the plot as an png
  62. Inputs :
  63. * y : array_like, the y axis values to be plotted
  64. * n : array_like, the x axis values to be plotted
  65. * name : string, name given to the saved png file
  66.  
  67. Creation : 14-02-2020
  68. """
  69. # Création de la figure, de taille fixe.
  70. plt.figure(figsize=(6,3))
  71.  
  72. (markerLines, stemLines, baseLines) = plt.stem(n,y,label="Signal")
  73.  
  74. plt.setp(markerLines,color="white",markeredgecolor = 'black')
  75. plt.xlabel("n (seconds)")
  76. plt.ylabel("y[n]")
  77. plt.title("Signal plot {}".format(name))
  78.  
  79. # Sauvegarde de la figure avec le bon noms.
  80. # Le second argument rétrécit les marges, par défaut relativement larges.
  81. plt.savefig(name + '.png', bbox_inches='tight')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement