Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. Since the data was not provided I have created my own data using numpy.random.randint().The
  2.  
  3. sample data is of the same shape as mentioned in our question statement.
  4.  
  5. Code for generating demo voltages.dat file for sample run
  6.  
  7. *********************************************************
  8.  
  9. input_file = 'voltages.dat'
  10.  
  11. '''
  12. given in your question statement that the file contains 10 data sets of voltages and
  13. each set contains 100 discrete values
  14.  
  15. so size = 100 X 10 where we have 100 rows and 10 columns
  16.  
  17. so i am mimicing this using numpy.random function
  18.  
  19. '''
  20.  
  21. demo_data = np.random.randint(low = 1,high = 20,size = (100,10))np.random.seed(10)
  22. file = open(input_file,'w')
  23.  
  24. for i in demo_data:
  25.  
  26. for j in i:
  27. file.write(str(j)+' ')
  28. file.write('\n')
  29.  
  30. file.close()
  31.  
  32. *********************************************************
  33.  
  34. step 1) libraries needed.
  35.  
  36. import math
  37.  
  38. import matplotlib.pyplot as plt
  39.  
  40. import numpy as np
  41.  
  42. #i have used numpy to save the voltage values form the file as it becomes more efficient when dealing with large data files and indexing also easy.
  43.  
  44. 1> RMS Function
  45. def rms(voltages):
  46.  
  47. if voltages == []:
  48. return None
  49.  
  50. n = len(voltages)
  51.  
  52. squared_list = list(map(lambda x:x**2,voltages))
  53.  
  54. return math.sqrt(sum(squared_list)/n)
  55.  
  56. 2> read_voltages function
  57.  
  58.  
  59. def read_voltages(name):
  60. print(name)
  61. try:
  62. file = open(name,'r');
  63.  
  64. content = file.readlines()
  65.  
  66. if content == []:
  67. return []
  68.  
  69. else:
  70. ls = []
  71. for i in content:
  72. #this is done to extract each element and convert it to an int
  73. ls.append(list(map(int,i.split())))
  74.  
  75.  
  76. voltages = np.array(ls)
  77.  
  78. '''
  79. calculating rms values.
  80. '''
  81.  
  82. #list to store average rms values
  83. rms_list = []
  84.  
  85. for i in range(voltages.shape[1]):
  86.  
  87. #each row is considered as a single dataset.
  88. #and each column is one dataset
  89. #there are 10 datasets this is given to us by shape 100 X 10
  90. #hence he iterate 10 times for each dataset (column)
  91.  
  92.  
  93. rmsval = rms(voltages[:,i])
  94.  
  95. rms_list.append(rmsval)
  96.  
  97.  
  98.  
  99. if len(rms_list) > 1:
  100. #this means more than 1 dataset
  101. #we need to take averge
  102. avg_rmsval = rms(rms_list)
  103. print_results(rms_list,avg_rmsval)
  104. name = input("enter name of the output file eg(a.out) format")
  105. save_result(name,rms_list,avg_rmsval)
  106. plot_results(rms_list,avg_rmsval)
  107. else:
  108. print_results(rms_list)
  109. name = input("enter name of the output file eg(a.out) format")
  110. save_result(name,rms_list)
  111. plot_results(rms_list)
  112.  
  113.  
  114. except IOError:
  115. print('IO error file not found..')
  116.  
  117.  
  118. 3>print_results function
  119.  
  120. def print_results(list_rms,avg_rms = -1):
  121.  
  122. print('Data Set rms(v)')
  123. for i,j in enumerate(list_rms):
  124.  
  125. #this num variable is used to calculate the width for the first column
  126. #it should be 10 ,1 space is taken by the no. eg 1 + ' '*9 is the first column.
  127. #but for 10 it will be 2 place for number and 8 spaces. calculated by formula 10-len(num)
  128.  
  129.  
  130. num = 1+i
  131. num = str(num)
  132. print(num,' '*(10-len(num)),j)
  133.  
  134. if avg_rms !=-1:
  135. print('\n\nThe average rms voltage is ',avg_rms)
  136.  
  137.  
  138. 4>save_result function
  139.  
  140. def save_result(name,rms_list,avg_rms = -1):
  141.  
  142. output_file = open(name,'w')
  143.  
  144. #%
  145. output_file.write('Data Set rms(v)\n')
  146.  
  147.  
  148. #list to store average rms values
  149.  
  150.  
  151. for i,j in enumerate(rms_list):
  152.  
  153. #each row is considered as a single dataset.
  154. #and each column is one dataset
  155. #there are 10 datasets this is given to us by shape 100 X 10
  156. #hence he iterate 10 times for each dataset (column)
  157.  
  158. num = i +1
  159. num =str(num)
  160.  
  161. #this num variable is used to calculate the width for the first column
  162. #it should be 10 ,1 space is taken by the no. eg 1 + ' '*9 is the first column.
  163. #but for 10 it will be 2 place for number and 8 spaces. calculated by formula 10-len(num)
  164.  
  165. #print(num,' '*(10-len(num)),rms(voltages[:,i]))
  166.  
  167. output_file.write(str(i+1)+' '*(10-len(num))+str(j)+"\n")
  168.  
  169. if avg_rms!=-1:
  170. #this means more than 1 dataset
  171. #we need to take averge
  172.  
  173. #print('\n\naverage rms voltage ',rms(avg_rms))
  174.  
  175.  
  176. output_file.write('\n\naverage rms voltage '+str(avg_rms))
  177.  
  178. output_file.close()
  179.  
  180.  
  181. 5>plot_result function
  182.  
  183. def plot_results(rms_list,avg_rms = -1):
  184.  
  185. n = len(rms_list)
  186.  
  187. if n > 1:
  188. x = range(n)
  189. y = rms_list
  190. plt.scatter(x,y,label = 'rms voltage')
  191. plt.scatter(x,[avg_rms]*n,c='r',label = 'average rms')
  192. plt.title('rms voltages for 10 data sets')
  193. plt.xlabel('Data Set')
  194. plt.ylabel('rms voltage,V')
  195. plt.legend()
  196.  
  197.  
  198. 6> main code:
  199.  
  200.  
  201. if __name__ == '__main__':
  202.  
  203. input_file = input("Enter name of input file. eg(voltages.dat) format")
  204. ret = read_voltages(input_file)
  205.  
  206. if ret == []:
  207. print('no. data found in the file..')
  208.  
  209.  
  210. Sample Output:
  211.  
  212. content of voltage.dat:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement