Example #1
0
def devils_staircase(num_octaves=7,
                     num_steps=12,
                     step_size=1,
                     hop=4096,
                     overlap=True,
                     center_freq=440,
                     band_width=150,
                     **params):
    """
    ::

        Generate an auditory illusion of an infinitely ascending/descending sequence of shepard tones
            num_octaves - number of sinusoidal octave bands to generate [7]
            num_steps - how many steps to take in the staircase
            step_size - semitone change per step, can be fractional [1.]
            hop - how many points to generate per step [12]
            overlap - whether the end-points should be cross-faded for overlap-add
            center_freq - where the peak of the spectrum will be [440]
            band_width - how wide a spectral band to use for shepard tones [150]
            **params - signal_params dict, see default_signal_params()

    """
    params = _check_signal_params(params)
    sr = params['sr']
    f0 = params['f0']
    norm_freq = 2 * pylab.pi / sr
    wlen = min([hop / 2, 2048])
    x = pylab.zeros(num_steps * hop + wlen)
    h = scipy.signal.hanning(wlen * 2)
    # overlap add
    params['num_points'] = hop + wlen
    phase_offset = 0
    for i in pylab.arange(num_steps):
        freq = f0 * 2**(((i * step_size) % 12) / 12.0)
        params['f0'] = freq
        s = shepard(num_octaves=num_octaves,
                    center_freq=center_freq,
                    band_width=band_width,
                    **params)
        s[0:wlen] = s[0:wlen] * h[0:wlen]
        s[hop:hop + wlen] = s[hop:hop + wlen] * h[wlen:wlen * 2]
        x[i * hop:(i + 1) * hop + wlen] = x[i * hop:(i + 1) * hop + wlen] + s
        phase_offset = phase_offset + hop * freq * norm_freq
    if not overlap:
        x = pylab.resize(x, num_steps * hop)
    x = balance_signal(x, 'maxabs')
    return x
Example #2
0
def pchip_eval(x, y, m, xvec):

    """
    Evaluate the piecewise cubic Hermite interpolant with monoticity preserved
    
        x = array containing the x-data
        y = array containing the y-data
        m = slopes at each (x,y) point [computed to preserve monotonicity]
        xnew = new "x" value where the interpolation is desired
    
        x must be sorted low to high... (no repeats)
        y can have repeated values
    
    This works with either a scalar or vector of "xvec"
    """
    n = len(x)
    mm = len(xvec)

    if not x_is_okay(x, xvec):                                                  # Make sure there aren't problems with the input data
        print "pchip_eval2() - ill formed 'x' vector!!!!!!!!!!!!!"

        STOPpchip_eval2                                                         # Cause a hard crash...
    """
    Find the indices "k" such that x[k] < xvec < x[k+1]
    """
    
    xx = P.resize(x,(mm,n)).transpose()                                         # Create "copies" of "x" as rows in a mxn 2-dimensional vector
    xxx = xx > xvec

    z = xxx[:-1,:] - xxx[1:,:]                                                  # Compute column by column differences

    k = z.argmax(axis=0)                                                        # Collapse over rows...

    h = x[k+1] - x[k]                                                           # Create the Hermite coefficients
    t = (xvec - x[k]) / h

    h00 = (2 * t**3) - (3 * t**2) + 1                                           # Hermite basis functions
    h10 =      t**3  - (2 * t**2) + t
    h01 = (-2* t**3) + (3 * t**2)
    h11 =      t**3  -      t**2

    ynew = h00*y[k] + h10*h*m[k] + h01*y[k+1] + h11*h*m[k+1]                    # Compute the interpolated value of "y"

    return ynew
Example #3
0
def devils_staircase(num_octaves=7, num_steps=12, step_size=1, hop=4096,
                     overlap=True, center_freq=440, band_width=150, **params):
    """
    ::

        Generate an auditory illusion of an infinitely ascending/descending sequence of shepard tones
            num_octaves - number of sinusoidal octave bands to generate [7]
            num_steps - how many steps to take in the staircase
            step_size - semitone change per step, can be fractional [1.]
            hop - how many points to generate per step [12]
            overlap - whether the end-points should be cross-faded for overlap-add
            center_freq - where the peak of the spectrum will be [440]
            band_width - how wide a spectral band to use for shepard tones [150]
            **params - signal_params dict, see default_signal_params()

    """
    params = _check_signal_params(**params)
    sr = params['sr']
    f0 = params['f0']
    norm_freq = 2 * pylab.pi / sr
    wlen = min([hop / 2, 2048])
    x = pylab.zeros(num_steps * hop + wlen)
    h = scipy.signal.hanning(wlen * 2)
    # overlap add
    params['num_points'] = hop + wlen
    phase_offset = 0
    for i in pylab.arange(num_steps):
        freq = f0 * 2**(((i * step_size) % 12) / 12.0)
        params['f0'] = freq
        s = shepard(num_octaves=num_octaves,
                    center_freq=center_freq, band_width=band_width, **params)
        s[0:wlen] = s[0:wlen] * h[0:wlen]
        s[hop:hop + wlen] = s[hop:hop + wlen] * h[wlen:wlen * 2]
        x[i * hop:(i + 1) * hop + wlen] = x[i * hop:(i + 1) * hop + wlen] + s
        phase_offset = phase_offset + hop * freq * norm_freq
    if not overlap:
        x = pylab.resize(x, num_steps * hop)
    x = balance_signal(x, 'maxabs')
    return x
Example #4
0
def devils_staircase(params, f0=441, num_octaves=7, num_steps=12, step_size=1, hop=4096, 
                     overlap=True, center_freq=440, band_width=150):
    """
    ::

        Generate an auditory illusion of an infinitely ascending/descending sequence of shepard tones
            params - parameter dict containing sr, and num_harmonics elements
            f0 - base frequency in Hertz of shepard tone [55]
            num_octaves - number of sinusoidal octave bands to generate [7]
            num_steps - how many steps to take in the staircase
            step_size - semitone change per step, can be fractional [1.]
            hop - how many points to generate per step
            overlap - whether the end-points should be cross-faded for overlap-add
            center_freq - where the peak of the spectrum will be
            band_width - how wide a spectral band to use for shepard tones
    """
    sr = params['sr']
    norm_freq = 2*pylab.pi/sr
    wlen = min([hop/2, 2048])
    print wlen
    x = pylab.zeros(num_steps*hop+wlen)
    h = scipy.signal.hanning(wlen*2)
    # overlap add    
    phase_offset=0
    for i in pylab.arange(num_steps):
        freq = f0*2**(((i*step_size)%12)/12.0)        
        s = shepard(params, f0=freq, num_octaves=num_octaves, num_points=hop+wlen, 
                    phase_offset=0, center_freq=center_freq, band_width=band_width)
        s[0:wlen] *= h[0:wlen]
        s[hop:hop+wlen] *= h[wlen:wlen*2]
        x[i*hop:(i+1)*hop+wlen] += s
        phase_offset += hop*freq*norm_freq
    if not overlap:
        x = pylab.resize(x, num_steps*hop)
    x /= pylab.rms_flat(x)
    return x
Example #5
0
def grid_points(x_range, y_range):
    from pylab import meshgrid, resize
    n = len(x_range) * len(y_range)
    x, y = meshgrid(x_range, y_range)
    return zip(resize(x,n).tolist(), resize(y,n).tolist())
Example #6
0
def bitsToImage(mat, shape):
    s = bitarray(squeeze(asarray(mat.transpose().flatten())).tolist()).tobytes()
    return resize(array(bytearray(s)).astype('uint8'), shape)
Example #7
0
x,y=dat[:,0],dat[:,1]

m13=(y[1:]-y[:-1])/(x[1:]-x[:-1])
m1=np.array(m13).tolist() #our slope
m1.append(-.585)
m=np.asarray(m1)
xvec=linspace(0,1,101)


n = len(x)
mm = len(xvec)

#find k such that x[k] < xvec < x[k+1]

# expand x to an nxm, array
xx = pb.resize(x,(mm,n)).transpose()
xxx = xx > xvec

#subtract subsueqent elemnents 
z = xxx[:-1,:] - xxx[1:,:]
#reduce to index array of size mm
k = z.argmax(axis=0)

#now plug in
h = x[k+1] - x[k]
z1 = (xvec - x[k]) / h[k]

k1=np.append(k[:-1],[8])

# Hermite functions
h0 =2*z1**3-3*z1**2+1