Voss 1/f noise algo help

kevin parks kp87 at lycos.com
Fri Mar 8 04:50:13 EST 2002


Hi I have a couple of attempts below at the beginnings of a 1/f^2
Noise generator. It is in a sort of heavily commented psuedo Python
(somewhat translated from PASCAL) You can see that there are some
areas where i am in way over my head.  I may have introduced a mistake
or two as well. Anyone familiar with Voss's algorithym want to help
smooth out the rough edges here? It sure would be nice to be able to
generate pink noise values in Python (brownian noise would be good
too, I am trying  to work something up on that too).

Back to the books,

<kp>


# An attempt to get R. F. Voss's algorithm ported to python

import random

def one_over_f (n=128):
	'''generates a sequence with a 1/f spectrum
	
	n determines the number of random generators and length of sequence
	'''
	seq_length = 2**n 		# Compute length of output sequence
	random_max = 1.0/n		# Set scale factor for random generators
	previous_seq_index = (seq_length - 1) 	# Initialize the previous
index value
	seq_index = 0		# Generate the 1/f seq
	while seq_index < seq_length:
		output_value = 0		# Generate output loop
		i = 0
		# Bit check loop
		# Compare bit 1 of seq_index with that of previous_seq_index
		# If they are not the same, generate new random number.
		while i < n:
			if bit_compare(i, seq_index) != bit_compare(i, previous_seq_index):
				values[i] = random(O, random_max)
			output_value = output_values + values[i]
			i = i + 1
			# End bit check loop
		previous_seq_index = seq_index
		seq_index = seq_index + 1
	return output-value
	
# ----

# def oneoverf(npts=128):
# 	nbits = 1
# 	np = 1
# 	nr = npts	# float
# 	nr = nr/2.0
# 	while nr > 1:
# 		nbits = nbits + 1
# 		np=2*np
# 		nr=nr/2
# 	for(kg=0; kg<nbits; kg++)
# 		rg[kg]=fran() #get random
# 	for(k=0; k<npts; k++)
# 		threshold=np
# 		ng=nbits
# 		while(k%threshold != 0):
# 			ng--
# 			threshold=threshold/2
# 		sum = 0
# 		for (kg=0; kg<nbits; kg++
# 			if(kg <ng):
# 				rg[kg] = fran()
# 			sum+=rg[kg]
# 		seqout[k]=sum/nbits
# 		
# 		# outputs scaled to range of 0 to 1
		
def oneoverf(npts=128):
	nbits = 1
	np = 1
	nr = npts	# float
	nr = nr/2.0
	while nr > 1:
		nbits = nbits + 1
		np=2*np
		nr=nr/2
	kg = 0
	while kg<nbits:
		rg[kg]=random.random() # get random
		kg = kg + 1
	k = 0
	while k<npts:
		threshold=np
		ng=nbits
		while k%threshold != 0 :
			ng = ng - 1
			threshold = (threshold/2)
		sum = 0
		kg=0
		while kg<nbits:
			if(kg <ng):
				rg[kg] = random.random()
			sum+=rg[kg]
		kg = kg +1
		k = k +1
		seqout[k]=sum/nbits
		
		# outputs scaled to range of 0 to 1



More information about the Python-list mailing list