Numpy combine channels
Wanderer
wanderer at dialup4less.com
Mon Sep 10 16:15:54 EDT 2012
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()
More information about the Python-list
mailing list