Help with numpy stereo sound array

299 views
Skip to first unread message

Martin

unread,
Jun 29, 2012, 8:14:56 AM6/29/12
to psychopy-users
Dear list!

This seems to be a fantastic piece of software. I am stuck however,
please could anyone tell me what I am doing wrong?

<code>
soundL = sin(2*pi*440*linspace(0,0.5,22050))
soundR = sin(2*pi*340*linspace(0,0.5,22050))

asound = np.transpose(np.vstack((soundL,soundR)))

print np.transpose(asound).shape
<\code>

asound is a 22050 by 2 numpy matrix that should come out as stereo but
it is played as a 44100 by 1 matrix (the two channels are played
sequentially!) I have tried both pyglet and pygame and a different
machine config so something seems to be wrong with my code.

Thanks in advance!

Alex Holcombe

unread,
Jul 1, 2012, 4:59:18 AM7/1/12
to psychopy-users
When I run your code, the last line reports that the matrix is 2 x
22050, not 22050 by 2. It should be N x 2, not 2 x N. That may be
your problem.

Also, I posted code that uses stereo here:
https://groups.google.com/group/psychopy-dev/browse_thread/thread/36849503bc9c0633
In the part that goes through psychopy, it should at least make one of
the two channels than the other.

Alex

Alex Holcombe

unread,
Jul 1, 2012, 5:00:45 AM7/1/12
to psychopy-users
I now see that for some reason you printed the shape of the transposed
matrix, not the original. Anyway, try my code.

good luck
Alex

On Jul 1, 6:59 pm, Alex Holcombe <aoholco...@gmail.com> wrote:
> When I run your code, the last line reports that the matrix is 2 x
> 22050, not 22050 by 2. It should be N x 2, not 2 x N.  That may be
> your problem.
>
> Also, I posted code that uses stereo here:https://groups.google.com/group/psychopy-dev/browse_thread/thread/368...

Martin

unread,
Jul 3, 2012, 8:49:07 AM7/3/12
to psychop...@googlegroups.com
Thanks Alex!

Your code seems to work as expected! Why do you set left channel to -1 when using PsychoPy? Try this in your code:

            if testPygame:
                val = int(round(val)) #takes as 16-bit signed integer
                buf[s][0] = -1   # left
            else:
                buf[s][0] = 0    # left
            buf[s][1] = val  #right
 

I added this to my code:
sound1 = zeros((22050,2))

and later on

for s in range(22050):
    sound1[s][0] = soundL[s] # left
    sound1[s][1] = soundR[s]  #right

I can't figure out why that works and

sound1 = np.transpose(np.vstack((soundL,soundR)))

doesn't.

Thanks again!

Alex Holcombe

unread,
Jul 4, 2012, 7:06:24 AM7/4/12
to psychopy-users
You're right, using 0 in the silent channel in the psychopy case gives
a better result. For some reason I was thinking that no signal would
correspond to -1; I'm not much of an auditory psychophysicist.

However, although using 0 in the psychopy case gives a better result,
approximately equal to the pygame case, I still hear the tone in the
channel that's supposed to be silent. Maybe that could simply be an
electromagnetic problem with cross-talk in my earphones? I'll get some
high-quality earphones or speaker connection and try it with that.

Alex

Alex Holcombe

unread,
Jul 4, 2012, 8:55:23 PM7/4/12
to psychopy-users
Wow what a great excuse this was to hook up some really nice and
expensive headphones!
With Beyerdynamic DT990 Pro headphones (~$180), it seems that the
signal is fully monaural when the array is set to all zeros in a
channel. The seeming problem of getting the signal in both channels
was apparently just electrical crosstalk :)

Alex

Jonathan Peirce

unread,
Jul 5, 2012, 2:55:57 AM7/5/12
to psychop...@googlegroups.com
Good news. Thanks for letting us know.
> --
> You received this message because you are subscribed to the Google Groups "psychopy-users" group.
> To post to this group, send email to psychop...@googlegroups.com.
> To unsubscribe from this group, send email to psychopy-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/psychopy-users?hl=en.
>
Reply all
Reply to author
Forward
0 new messages