# Script to produce simple plots to demonstrate how stacking spectra improves sensitivity import numpy import matplotlib speclength = 20 # Number of elements in each spectrum nspec = 100 # Number of spectra to generate and plot # Each column will be one fake spectrum. The stacked values will be computed on the fly afterwards. mainspec = numpy.zeros((speclength, nspec)) for i in range(nspec): # Loc = mean, scale = standard deviation, size = number of elements newspec = numpy.random.normal(loc=0.0, scale=1.0, size=speclength) mainspec[:,i] = newspec[:] # Print the rms of each spectrum as we go #print(numpy.std(mainspec[:,i])) # Do the stacking for each column stackplot = [] for i in range(1, nspec+1): # Extract an array going from 0 to the ith column, then sum each row (axis = 1) stacked = numpy.sum(mainspec[:,0:i], axis=1) / float(i) stackplot.append([i, numpy.std(stacked)]) stackplot = numpy.asarray(stackplot) stackedplotfile = open('StackingPlot_'+str(speclength)+'.txt','w') for i in range(len(stackplot)): stackedplotfile.write(str(stackplot[i,0])+' '+str(stackplot[i,1])+'\n') stackedplotfile.close() # We can extract a range of specific columnns from the array like so : # mainspec[:,start:end] # Not that this works like a for loop, excluding the last value !