Reputation: 11
I'm trying to create an array of 256 stimuli that represents the frequency value to input into my sound stimuli. So far I have created an array of 4 numbers representing the 4 different frequency levels for my audio tones:
#Pitch list - create an array from 1 to 4 repeated for 256 stimuli
pitch_list = [1,2,3,4]
new_pitch_list = np.repeat(pitch_list,64)
random.shuffle(new_pitch_list)
print(new_pitch_list)
#Replace 1-4 integers in new_pitch_list with frequency values
for x in range(0,len(new_pitch_list)):
if new_pitch_list[x] == 1:
new_pitch_list[x] = 500
elif new_pitch_list[x] == 2:
new_pitch_list[x] = 62
elif new_pitch_list[x] == 3:
new_pitch_list[x] = 750
else:
new_pitch_list[x] == 4
new_pitch_list[x] = 875
My code works for randomly producing an array of 256 numbers of which there are 4 possibilities (500, 625, 750, 875). However, my problem is that I need to create the new_pitch_list so there are no repetitions of 2 numbers. I need to do this so the frequency of the audio tones isn't the same for consecutive audio tones.
I understand that I may need to change the way I use the random.shuffle function, however, I'm not sure if I also need to change my for loop as well to make this work.
So far I have tried to replace the random.shuffle function with the random.choice function, but I'm not sure if I'm going in the wrong direction.Because I'm still fairly new to Python coding, I'm not sure if I can solve this problem without having to change my for loop, so any help would be greatly appreciated!
Upvotes: 1
Views: 656
Reputation: 780899
After you assign each value, remove that value from the list of choices, and use random.choice()
.
pitches = [600, 62, 750, 875]
last_pitch = random.choice(pitches)
new_pitch_list = [last_pitch]
for _ in range(255):
pitches.remove(last_pitch)
pitch = random.choice(pitches)
new_pitch_list.append(pitch)
pitches.append(last_pitch)
Upvotes: 1
Reputation: 11267
I would make it so that you populate your array with 3 of your 4 values, and then each time you see consecutive duplicate values you replace the second one with the 4th value. Something like this (untested, but you get the gist).
Also - I'd cut out some of the lines you don't need:
new_pitch_list = np.repeat([500, 62, 750],64)
random.shuffle(new_pitch_list)
print(new_pitch_list)
#Replace 1-4 integers in new_pitch_list with frequency values
for x in range(1,len(new_pitch_list)):
if(new_pitch_list[x-1] == new_pitch_list[x]):
new_pitch_list[x] = 875
Upvotes: 1