1

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!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Evan Canny
  • 11
  • 1
  • 3
    Welcome to Stack Overflow. Please read [ask] and the [formatting help](https://stackoverflow.com/help/formatting), and make sure your code appears in the post exactly as you actually have it. Then try to explain the problem more clearly, by showing: what happens when you run the code? How is that different from what should happen? What exactly do you mean about "repetitions", given that you are shuffling data that includes repeats? What are the actual requirements for the data - for example, do there actually need to be 64 occurrences of each value? – Karl Knechtel Mar 23 '22 at 15:46
  • @KarlKnechtel The explanation of the requirement makes it clear that he means "no consecutive repetitions". – Barmar Mar 23 '22 at 15:50
  • My point is, if "equal numbers of each value, and equal probability of each possible outcome" are requirements, the problem becomes considerably harder. In your solution you appear to have assumed they are not. – Karl Knechtel Mar 23 '22 at 16:09

2 Answers2

1

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
mikeb
  • 10,578
  • 7
  • 62
  • 120
1

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)
Barmar
  • 741,623
  • 53
  • 500
  • 612