Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28845 > unrolled thread

Numpy combine channels

Started byWanderer <wanderer@dialup4less.com>
First post2012-09-10 12:39 -0700
Last post2012-11-10 08:05 -0500
Articles 10 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Numpy combine channels Wanderer <wanderer@dialup4less.com> - 2012-09-10 12:39 -0700
    Re: Numpy combine channels Wanderer <wanderer@dialup4less.com> - 2012-09-10 12:56 -0700
    RE: Numpy combine channels Nick Cash <nick.cash@npcinternational.com> - 2012-09-10 19:50 +0000
    Re: Numpy combine channels MRAB <python@mrabarnett.plus.com> - 2012-09-10 21:11 +0100
      Re: Numpy combine channels Wanderer <wanderer@dialup4less.com> - 2012-09-10 13:14 -0700
        Re: Numpy combine channels Wanderer <wanderer@dialup4less.com> - 2012-09-10 13:15 -0700
        Re: Numpy combine channels Wanderer <wanderer@dialup4less.com> - 2012-09-10 13:15 -0700
      Re: Numpy combine channels Wanderer <wanderer@dialup4less.com> - 2012-09-10 13:14 -0700
      Re: Numpy combine channels aahz@pythoncraft.com (Aahz) - 2012-11-09 20:30 -0800
        Re: Numpy combine channels Dave Angel <d@davea.name> - 2012-11-10 08:05 -0500

#28845 — Numpy combine channels

FromWanderer <wanderer@dialup4less.com>
Date2012-09-10 12:39 -0700
SubjectNumpy combine channels
Message-ID<de5d9415-f3d8-47b9-bcc2-822dc57fd1d8@googlegroups.com>
I have an array generated by audiolab of left and right stereo channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine the left and right channels to get an array [2,3,5]. Is there a numpy command to do that?

Thanks

[toc] | [next] | [standalone]


#28846

FromWanderer <wanderer@dialup4less.com>
Date2012-09-10 12:56 -0700
Message-ID<c8f58c78-b16b-4d86-ae1a-604d904fa757@googlegroups.com>
In reply to#28845
On Monday, September 10, 2012 3:39:11 PM UTC-4, Wanderer wrote:
> I have an array generated by audiolab of left and right stereo channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine the left and right channels to get an array [2,3,5]. Is there a numpy command to do that?
> 
> 
> 
> Thanks

I figured it out. numpy.sum(array, axis=1). 

[toc] | [prev] | [next] | [standalone]


#28847

FromNick Cash <nick.cash@npcinternational.com>
Date2012-09-10 19:50 +0000
Message-ID<mailman.464.1347307538.27098.python-list@python.org>
In reply to#28845
> I have an array generated by audiolab of left and right stereo
> channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine
> the left and right channels to get an array [2,3,5]. Is there a numpy
> command to do that?

You may be over-thinking this, and numpy might not be necessary.

A simple solution would be just a quick list comprehension:

stereo_array = [[1, 1], [1, 2], [2, 3]]
mono_array = [l+r for (l, r) in stereo_array]

Thanks,
Nick Cash

[toc] | [prev] | [next] | [standalone]


#28848

FromMRAB <python@mrabarnett.plus.com>
Date2012-09-10 21:11 +0100
Message-ID<mailman.465.1347307911.27098.python-list@python.org>
In reply to#28845
On 10/09/2012 20:39, Wanderer wrote:
> I have an array generated by audiolab of left and right stereo
> channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine
> the left and right channels to get an array [2,3,5]. Is there a numpy
> command to do that?
>
>>> import numpy
>>> numpy.array([[1,1],[1,2],[2,3]], dtype="i")
array([[1, 1],
        [1, 2],
        [2, 3]])
>>> a[:, 0]
array([1, 1, 2])
>>> a[:, 1]
array([1, 2, 3])
>>> a[:, 0] + a[:, 1]
array([2, 3, 5])

But should they be added together to make mono?

Suppose, for example, that both channels have a maximum value. Their
sum would be _twice_ the maximum.

Therefore, I think that it should probably be the average.

 >>> (a[:, 0] + a[:, 1]) / 2
array([1, 1, 2])

[toc] | [prev] | [next] | [standalone]


#28849

FromWanderer <wanderer@dialup4less.com>
Date2012-09-10 13:14 -0700
Message-ID<0d091492-39e0-42b3-a383-9853af33136f@googlegroups.com>
In reply to#28848
On Monday, September 10, 2012 4:12:40 PM UTC-4, MRAB wrote:
> On 10/09/2012 20:39, Wanderer wrote:
> 
> > I have an array generated by audiolab of left and right stereo
> 
> > channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine
> 
> > the left and right channels to get an array [2,3,5]. Is there a numpy
> 
> > command to do that?
> 
> >
> 
> >>> import numpy
> 
> >>> numpy.array([[1,1],[1,2],[2,3]], dtype="i")
> 
> array([[1, 1],
> 
>         [1, 2],
> 
>         [2, 3]])
> 
> >>> a[:, 0]
> 
> array([1, 1, 2])
> 
> >>> a[:, 1]
> 
> array([1, 2, 3])
> 
> >>> a[:, 0] + a[:, 1]
> 
> array([2, 3, 5])
> 
> 
> 
> But should they be added together to make mono?
> 
> 
> 
> Suppose, for example, that both channels have a maximum value. Their
> 
> sum would be _twice_ the maximum.
> 
> 
> 
> Therefore, I think that it should probably be the average.
> 
> 
> 
>  >>> (a[:, 0] + a[:, 1]) / 2
> 
> array([1, 1, 2])

I'm decoding morse code. So it's CV dots and dashes.

[toc] | [prev] | [next] | [standalone]


#28852

FromWanderer <wanderer@dialup4less.com>
Date2012-09-10 13:15 -0700
Message-ID<45a73671-552e-491e-a1ff-27d7a3fde52e@googlegroups.com>
In reply to#28849
On Monday, September 10, 2012 4:14:18 PM UTC-4, Wanderer wrote:
> On Monday, September 10, 2012 4:12:40 PM UTC-4, MRAB wrote:
> 
> > On 10/09/2012 20:39, Wanderer wrote:
> 
> > 
> 
> > > I have an array generated by audiolab of left and right stereo
> 
> > 
> 
> > > channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine
> 
> > 
> 
> > > the left and right channels to get an array [2,3,5]. Is there a numpy
> 
> > 
> 
> > > command to do that?
> 
> > 
> 
> > >
> 
> > 
> 
> > >>> import numpy
> 
> > 
> 
> > >>> numpy.array([[1,1],[1,2],[2,3]], dtype="i")
> 
> > 
> 
> > array([[1, 1],
> 
> > 
> 
> >         [1, 2],
> 
> > 
> 
> >         [2, 3]])
> 
> > 
> 
> > >>> a[:, 0]
> 
> > 
> 
> > array([1, 1, 2])
> 
> > 
> 
> > >>> a[:, 1]
> 
> > 
> 
> > array([1, 2, 3])
> 
> > 
> 
> > >>> a[:, 0] + a[:, 1]
> 
> > 
> 
> > array([2, 3, 5])
> 
> > 
> 
> > 
> 
> > 
> 
> > But should they be added together to make mono?
> 
> > 
> 
> > 
> 
> > 
> 
> > Suppose, for example, that both channels have a maximum value. Their
> 
> > 
> 
> > sum would be _twice_ the maximum.
> 
> > 
> 
> > 
> 
> > 
> 
> > Therefore, I think that it should probably be the average.
> 
> > 
> 
> > 
> 
> > 
> 
> >  >>> (a[:, 0] + a[:, 1]) / 2
> 
> > 
> 
> > array([1, 1, 2])
> 
> 
> 
> I'm decoding morse code. So it's CV dots and dashes.

In case anyone is interested, here is the full code.

# morsecode.py
import numpy as np
from scikits.audiolab import wavread
from scipy.signal import decimate
from pylab import plot
from pylab import show
import os

def movingaverage(interval, window_size):
    window = np.ones(int(window_size)) / float(window_size)
    return np.convolve(interval, window, 'same')

def wav2morse(resultDir, filename):
    """ Convert a wave file to morse code
    resultDir: directory for wave file and results
    filename: wave file name

    """
    data, _fs, _enc = wavread(resultDir + '\\' + filename)
    data = np.sum(data, axis=1)
    data = np.fabs(data)
    data = movingaverage(data, 100)
    data = decimate(data, 2)
    highcount = 0
    lowcount = 0
    fileBase, _fileExt = os.path.splitext(filename)
    f = open(resultDir + '\\' + fileBase + '.txt', 'w')
    for d in data:
        if d > 0.3:
            if lowcount > 3000:
                f.write(' ')
            lowcount = 0
            highcount += 1
        else:
            if highcount > 3000:
                f.write('-')
            elif highcount > 1000:
                f.write('.')
            highcount = 0
            lowcount += 1
    f.close()

[toc] | [prev] | [next] | [standalone]


#28853

FromWanderer <wanderer@dialup4less.com>
Date2012-09-10 13:15 -0700
Message-ID<mailman.467.1347308157.27098.python-list@python.org>
In reply to#28849
On Monday, September 10, 2012 4:14:18 PM UTC-4, Wanderer wrote:
> On Monday, September 10, 2012 4:12:40 PM UTC-4, MRAB wrote:
> 
> > On 10/09/2012 20:39, Wanderer wrote:
> 
> > 
> 
> > > I have an array generated by audiolab of left and right stereo
> 
> > 
> 
> > > channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine
> 
> > 
> 
> > > the left and right channels to get an array [2,3,5]. Is there a numpy
> 
> > 
> 
> > > command to do that?
> 
> > 
> 
> > >
> 
> > 
> 
> > >>> import numpy
> 
> > 
> 
> > >>> numpy.array([[1,1],[1,2],[2,3]], dtype="i")
> 
> > 
> 
> > array([[1, 1],
> 
> > 
> 
> >         [1, 2],
> 
> > 
> 
> >         [2, 3]])
> 
> > 
> 
> > >>> a[:, 0]
> 
> > 
> 
> > array([1, 1, 2])
> 
> > 
> 
> > >>> a[:, 1]
> 
> > 
> 
> > array([1, 2, 3])
> 
> > 
> 
> > >>> a[:, 0] + a[:, 1]
> 
> > 
> 
> > array([2, 3, 5])
> 
> > 
> 
> > 
> 
> > 
> 
> > But should they be added together to make mono?
> 
> > 
> 
> > 
> 
> > 
> 
> > Suppose, for example, that both channels have a maximum value. Their
> 
> > 
> 
> > sum would be _twice_ the maximum.
> 
> > 
> 
> > 
> 
> > 
> 
> > Therefore, I think that it should probably be the average.
> 
> > 
> 
> > 
> 
> > 
> 
> >  >>> (a[:, 0] + a[:, 1]) / 2
> 
> > 
> 
> > array([1, 1, 2])
> 
> 
> 
> I'm decoding morse code. So it's CV dots and dashes.

In case anyone is interested, here is the full code.

# morsecode.py
import numpy as np
from scikits.audiolab import wavread
from scipy.signal import decimate
from pylab import plot
from pylab import show
import os

def movingaverage(interval, window_size):
    window = np.ones(int(window_size)) / float(window_size)
    return np.convolve(interval, window, 'same')

def wav2morse(resultDir, filename):
    """ Convert a wave file to morse code
    resultDir: directory for wave file and results
    filename: wave file name

    """
    data, _fs, _enc = wavread(resultDir + '\\' + filename)
    data = np.sum(data, axis=1)
    data = np.fabs(data)
    data = movingaverage(data, 100)
    data = decimate(data, 2)
    highcount = 0
    lowcount = 0
    fileBase, _fileExt = os.path.splitext(filename)
    f = open(resultDir + '\\' + fileBase + '.txt', 'w')
    for d in data:
        if d > 0.3:
            if lowcount > 3000:
                f.write(' ')
            lowcount = 0
            highcount += 1
        else:
            if highcount > 3000:
                f.write('-')
            elif highcount > 1000:
                f.write('.')
            highcount = 0
            lowcount += 1
    f.close()

[toc] | [prev] | [next] | [standalone]


#28851

FromWanderer <wanderer@dialup4less.com>
Date2012-09-10 13:14 -0700
Message-ID<mailman.466.1347308061.27098.python-list@python.org>
In reply to#28848
On Monday, September 10, 2012 4:12:40 PM UTC-4, MRAB wrote:
> On 10/09/2012 20:39, Wanderer wrote:
> 
> > I have an array generated by audiolab of left and right stereo
> 
> > channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine
> 
> > the left and right channels to get an array [2,3,5]. Is there a numpy
> 
> > command to do that?
> 
> >
> 
> >>> import numpy
> 
> >>> numpy.array([[1,1],[1,2],[2,3]], dtype="i")
> 
> array([[1, 1],
> 
>         [1, 2],
> 
>         [2, 3]])
> 
> >>> a[:, 0]
> 
> array([1, 1, 2])
> 
> >>> a[:, 1]
> 
> array([1, 2, 3])
> 
> >>> a[:, 0] + a[:, 1]
> 
> array([2, 3, 5])
> 
> 
> 
> But should they be added together to make mono?
> 
> 
> 
> Suppose, for example, that both channels have a maximum value. Their
> 
> sum would be _twice_ the maximum.
> 
> 
> 
> Therefore, I think that it should probably be the average.
> 
> 
> 
>  >>> (a[:, 0] + a[:, 1]) / 2
> 
> array([1, 1, 2])

I'm decoding morse code. So it's CV dots and dashes.

[toc] | [prev] | [next] | [standalone]


#33075

Fromaahz@pythoncraft.com (Aahz)
Date2012-11-09 20:30 -0800
Message-ID<k7kl8b$5g0$1@panix5.panix.com>
In reply to#28848
In article <mailman.465.1347307911.27098.python-list@python.org>,
MRAB  <python-list@python.org> wrote:
>On 10/09/2012 20:39, Wanderer wrote:
>>
>> I have an array generated by audiolab of left and right stereo
>> channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine
>> the left and right channels to get an array [2,3,5]. Is there a numpy
>> command to do that?
>
>>>> import numpy
>>>> numpy.array([[1,1],[1,2],[2,3]], dtype="i")
>array([[1, 1],
>        [1, 2],
>        [2, 3]])
>>>> a[:, 0]
>array([1, 1, 2])
>>>> a[:, 1]
>array([1, 2, 3])
>>>> a[:, 0] + a[:, 1]
>array([2, 3, 5])
>
>But should they be added together to make mono?
>
>Suppose, for example, that both channels have a maximum value. Their
>sum would be _twice_ the maximum.
>
>Therefore, I think that it should probably be the average.
>
> >>> (a[:, 0] + a[:, 1]) / 2
>array([1, 1, 2])

I'd actually think it should be the max.  Consider a stereo where one
side is playing a booming bass while the other side is playing a rest
note -- should the mono combination be half as loud as as the bass?
-- 
Aahz (aahz@pythoncraft.com)           <*>         http://www.pythoncraft.com/

"....Normal is what cuts off your sixth finger and your tail..."  --Siobhan

[toc] | [prev] | [next] | [standalone]


#33085

FromDave Angel <d@davea.name>
Date2012-11-10 08:05 -0500
Message-ID<mailman.3534.1352552737.27098.python-list@python.org>
In reply to#33075
On 11/09/2012 11:30 PM, Aahz wrote:
> In article <mailman.465.1347307911.27098.python-list@python.org>,
> MRAB  <python-list@python.org> wrote:
>> <snip>
>>
>> But should they be added together to make mono?
>>
>> Suppose, for example, that both channels have a maximum value. Their
>> sum would be _twice_ the maximum.
>>
>> Therefore, I think that it should probably be the average.
>>
>>>>> (a[:, 0] + a[:, 1]) / 2
>> array([1, 1, 2])
> I'd actually think it should be the max.  Consider a stereo where one
> side is playing a booming bass while the other side is playing a rest
> note -- should the mono combination be half as loud as as the bass?

max would sound awful.

The right answer is to add them with weighting, then scale the whole
waveform according to a new calculation of clipping. Just like a mixer,
you have level controls on each input, then an overall gain.

So if the inputs were x and y, the output would be   gain *( x_scale * x
+ y_scale * y), but it'd normally be done in two passes, so as to
minimize the places that are clipped, while maximizing the average. 
it's also possible to have gain vary across the time axis, like an agc. 
But you wouldn't want that as a default, as it'd destroy the dynamics of
a musical piece.

One more consideration.  If these are unsigned values (eg. 0 to 255),
then you should adjust both signals by 128 before storing them as signed
values, do your arithmetic, and then adjust again by adding 128.  You
could do the algebraic equivalent, but the programming would be much
simpler on signed values.



-- 

DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web