Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Since the data was not provided I have created my own data using numpy.random.randint().The
- sample data is of the same shape as mentioned in our question statement.
- Code for generating demo voltages.dat file for sample run
- *********************************************************
- input_file = 'voltages.dat'
- '''
- given in your question statement that the file contains 10 data sets of voltages and
- each set contains 100 discrete values
- so size = 100 X 10 where we have 100 rows and 10 columns
- so i am mimicing this using numpy.random function
- '''
- demo_data = np.random.randint(low = 1,high = 20,size = (100,10))np.random.seed(10)
- file = open(input_file,'w')
- for i in demo_data:
- for j in i:
- file.write(str(j)+' ')
- file.write('\n')
- file.close()
- *********************************************************
- step 1) libraries needed.
- import math
- import matplotlib.pyplot as plt
- import numpy as np
- #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.
- 1> RMS Function
- def rms(voltages):
- if voltages == []:
- return None
- n = len(voltages)
- squared_list = list(map(lambda x:x**2,voltages))
- return math.sqrt(sum(squared_list)/n)
- 2> read_voltages function
- def read_voltages(name):
- print(name)
- try:
- file = open(name,'r');
- content = file.readlines()
- if content == []:
- return []
- else:
- ls = []
- for i in content:
- #this is done to extract each element and convert it to an int
- ls.append(list(map(int,i.split())))
- voltages = np.array(ls)
- '''
- calculating rms values.
- '''
- #list to store average rms values
- rms_list = []
- for i in range(voltages.shape[1]):
- #each row is considered as a single dataset.
- #and each column is one dataset
- #there are 10 datasets this is given to us by shape 100 X 10
- #hence he iterate 10 times for each dataset (column)
- rmsval = rms(voltages[:,i])
- rms_list.append(rmsval)
- if len(rms_list) > 1:
- #this means more than 1 dataset
- #we need to take averge
- avg_rmsval = rms(rms_list)
- print_results(rms_list,avg_rmsval)
- name = input("enter name of the output file eg(a.out) format")
- save_result(name,rms_list,avg_rmsval)
- plot_results(rms_list,avg_rmsval)
- else:
- print_results(rms_list)
- name = input("enter name of the output file eg(a.out) format")
- save_result(name,rms_list)
- plot_results(rms_list)
- except IOError:
- print('IO error file not found..')
- 3>print_results function
- def print_results(list_rms,avg_rms = -1):
- print('Data Set rms(v)')
- for i,j in enumerate(list_rms):
- #this num variable is used to calculate the width for the first column
- #it should be 10 ,1 space is taken by the no. eg 1 + ' '*9 is the first column.
- #but for 10 it will be 2 place for number and 8 spaces. calculated by formula 10-len(num)
- num = 1+i
- num = str(num)
- print(num,' '*(10-len(num)),j)
- if avg_rms !=-1:
- print('\n\nThe average rms voltage is ',avg_rms)
- 4>save_result function
- def save_result(name,rms_list,avg_rms = -1):
- output_file = open(name,'w')
- #%
- output_file.write('Data Set rms(v)\n')
- #list to store average rms values
- for i,j in enumerate(rms_list):
- #each row is considered as a single dataset.
- #and each column is one dataset
- #there are 10 datasets this is given to us by shape 100 X 10
- #hence he iterate 10 times for each dataset (column)
- num = i +1
- num =str(num)
- #this num variable is used to calculate the width for the first column
- #it should be 10 ,1 space is taken by the no. eg 1 + ' '*9 is the first column.
- #but for 10 it will be 2 place for number and 8 spaces. calculated by formula 10-len(num)
- #print(num,' '*(10-len(num)),rms(voltages[:,i]))
- output_file.write(str(i+1)+' '*(10-len(num))+str(j)+"\n")
- if avg_rms!=-1:
- #this means more than 1 dataset
- #we need to take averge
- #print('\n\naverage rms voltage ',rms(avg_rms))
- output_file.write('\n\naverage rms voltage '+str(avg_rms))
- output_file.close()
- 5>plot_result function
- def plot_results(rms_list,avg_rms = -1):
- n = len(rms_list)
- if n > 1:
- x = range(n)
- y = rms_list
- plt.scatter(x,y,label = 'rms voltage')
- plt.scatter(x,[avg_rms]*n,c='r',label = 'average rms')
- plt.title('rms voltages for 10 data sets')
- plt.xlabel('Data Set')
- plt.ylabel('rms voltage,V')
- plt.legend()
- 6> main code:
- if __name__ == '__main__':
- input_file = input("Enter name of input file. eg(voltages.dat) format")
- ret = read_voltages(input_file)
- if ret == []:
- print('no. data found in the file..')
- Sample Output:
- content of voltage.dat:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement