~ubuntu-branches/ubuntu/oneiric/python-scipy/oneiric-proposed

« back to all changes in this revision

Viewing changes to scipy/signal/waveforms.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 21:26:25 UTC
  • mfrom: (9.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110406212625-3izdplobqe6fzeql
Tags: 0.9.0+dfsg1-1
* New upstream release (Closes: #614407, #579041, #569008)
* Convert to dh_python2 (Closes: #617028)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
#   Rewrote much of chirp()
6
6
#   Added sweep_poly()
7
7
 
8
 
import warnings
9
8
from numpy import asarray, zeros, place, nan, mod, pi, extract, log, sqrt, \
10
 
     exp, cos, sin, polyval, polyint, size, log10
 
9
     exp, cos, sin, polyval, polyint
11
10
 
12
 
def sawtooth(t,width=1):
 
11
def sawtooth(t, width=1):
13
12
    """
14
13
    Return a periodic sawtooth waveform.
15
14
 
69
68
    return y
70
69
 
71
70
 
72
 
def square(t,duty=0.5):
 
71
def square(t, duty=0.5):
73
72
    """
74
73
    Return a periodic square-wave waveform.
75
74
 
121
120
    place(y,mask3,-1)
122
121
    return y
123
122
 
124
 
def gausspulse(t,fc=1000,bw=0.5,bwr=-6,tpr=-60,retquad=0,retenv=0):
 
123
def gausspulse(t, fc=1000, bw=0.5, bwr=-6, tpr=-60, retquad=False, retenv=False):
125
124
    """
126
 
    Return a gaussian modulated sinusoid: exp(-a t^2) exp(1j*2*pi*fc).
 
125
    Return a gaussian modulated sinusoid: exp(-a t^2) exp(1j*2*pi*fc*t).
127
126
 
128
 
    If `retquad` is non-zero, then return the real and imaginary parts
129
 
    (in-phase and quadrature)
130
 
    If `retenv` is non-zero, then return the envelope (unmodulated signal).
 
127
    If `retquad` is True, then return the real and imaginary parts
 
128
    (in-phase and quadrature).
 
129
    If `retenv` is True, then return the envelope (unmodulated signal).
131
130
    Otherwise, return the real part of the modulated sinusoid.
132
131
 
133
132
    Parameters
134
133
    ----------
135
 
    t : ndarray
 
134
    t : ndarray, or the string 'cutoff'
136
135
        Input array.
137
136
    fc : int, optional
138
 
        Center frequency (Hz).
 
137
        Center frequency (Hz).  Default is 1000.
139
138
    bw : float, optional
140
139
        Fractional bandwidth in frequency domain of pulse (Hz).
 
140
        Default is 0.5.
141
141
    bwr: float, optional
142
142
        Reference level at which fractional bandwidth is calculated (dB).
 
143
        Default is -6.
143
144
    tpr : float, optional
144
145
        If `t` is 'cutoff', then the function returns the cutoff
145
146
        time for when the pulse amplitude falls below `tpr` (in dB).
146
 
    retquad : int, optional
147
 
        Return the quadrature (imaginary) as well as the real part
148
 
        of the signal.
149
 
    retenv : int, optional
150
 
        Return the envelope of the signal.
 
147
        Default is -60.
 
148
    retquad : bool, optional
 
149
        If True, return the quadrature (imaginary) as well as the real part
 
150
        of the signal.  Default is False.
 
151
    retenv : bool, optional
 
152
        If True, return the envelope of the signal.  Default is False.
151
153
 
152
154
    """
153
155
    if fc < 0:
154
 
        raise ValueError, "Center frequency (fc=%.2f) must be >=0." % fc
 
156
        raise ValueError("Center frequency (fc=%.2f) must be >=0." % fc)
155
157
    if bw <= 0:
156
 
        raise ValueError, "Fractional bandwidth (bw=%.2f) must be > 0." % bw
 
158
        raise ValueError("Fractional bandwidth (bw=%.2f) must be > 0." % bw)
157
159
    if bwr >= 0:
158
 
        raise ValueError, "Reference level for bandwidth (bwr=%.2f) must " \
159
 
              "be < 0 dB" % bwr
 
160
        raise ValueError("Reference level for bandwidth (bwr=%.2f) must "
 
161
              "be < 0 dB" % bwr)
160
162
 
161
163
    # exp(-a t^2) <->  sqrt(pi/a) exp(-pi^2/a * f^2)  = g(f)
162
164
 
170
172
        #  Solve exp(-a tc**2) = tref  for tc
171
173
        #   tc = sqrt(-log(tref) / a) where tref = 10^(tpr/20)
172
174
        if tpr >= 0:
173
 
            raise ValueError, "Reference level for time cutoff must be < 0 dB"
 
175
            raise ValueError("Reference level for time cutoff must be < 0 dB")
174
176
        tref = pow(10.0, tpr / 20.0)
175
177
        return sqrt(-log(tref)/a)
176
178
 
187
189
        return yI, yQ, yenv
188
190
 
189
191
 
190
 
# This is chirp from scipy 0.7:
191
 
 
192
 
def old_chirp(t, f0=0, t1=1, f1=100, method='linear', phi=0, qshape=None):
193
 
    """Frequency-swept cosine generator.
194
 
 
195
 
    Parameters
196
 
    ----------
197
 
    t : ndarray
198
 
        Times at which to evaluate the waveform.
199
 
    f0 : float or ndarray, optional
200
 
        Frequency (in Hz) of the waveform at time 0.  If `f0` is an
201
 
        ndarray, it specifies the frequency change as a polynomial in
202
 
        `t` (see Notes below).
203
 
    t1 : float, optional
204
 
        Time at which `f1` is specified.
205
 
    f1 : float, optional
206
 
        Frequency (in Hz) of the waveform at time `t1`.
207
 
    method : {'linear', 'quadratic', 'logarithmic'}, optional
208
 
        Kind of frequency sweep.
209
 
    phi : float
210
 
        Phase offset, in degrees.
211
 
    qshape : {'convex', 'concave'}
212
 
        If method is 'quadratic', `qshape` specifies its shape.
213
 
 
214
 
    Notes
215
 
    -----
216
 
    If `f0` is an array, it forms the coefficients of a polynomial in
217
 
    `t` (see `numpy.polval`). The polynomial determines the waveform
218
 
    frequency change in time.  In this case, the values of `f1`, `t1`,
219
 
    `method`, and `qshape` are ignored.
220
 
 
221
 
    This function is deprecated.  It will be removed in SciPy version 0.9.0.
222
 
    It exists so that during in version 0.8.0, the new chirp function can
223
 
    call this function to preserve the old behavior of the quadratic chirp.
224
 
    """
225
 
    warnings.warn("The function old_chirp is deprecated, and will be removed in "
226
 
                    "SciPy 0.9", DeprecationWarning)
227
 
    # Convert to radians.
228
 
    phi *= pi / 180
229
 
    if size(f0) > 1:
230
 
        # We were given a polynomial.
231
 
        return cos(2*pi*polyval(polyint(f0),t)+phi)
232
 
    if method in ['linear','lin','li']:
233
 
        beta = (f1-f0)/t1
234
 
        phase_angle = 2*pi * (f0*t + 0.5*beta*t*t)
235
 
    elif method in ['quadratic','quad','q']:
236
 
        if qshape == 'concave':
237
 
            mxf = max(f0,f1)
238
 
            mnf = min(f0,f1)
239
 
            f1,f0 = mxf, mnf
240
 
        elif qshape == 'convex':
241
 
            mxf = max(f0,f1)
242
 
            mnf = min(f0,f1)
243
 
            f1,f0 = mnf, mxf
244
 
        else:
245
 
            raise ValueError("qshape must be either 'concave' or 'convex' but "
246
 
                "a value of %r was given." % qshape)
247
 
        beta = (f1-f0)/t1/t1
248
 
        phase_angle = 2*pi * (f0*t + beta*t*t*t/3)
249
 
    elif method in ['logarithmic','log','lo']:
250
 
        if f1 <= f0:
251
 
            raise ValueError(
252
 
                "For a logarithmic sweep, f1=%f must be larger than f0=%f."
253
 
                % (f1, f0))
254
 
        beta = log10(f1-f0)/t1
255
 
        phase_angle = 2*pi * (f0*t + (pow(10,beta*t)-1)/(beta*log(10)))
256
 
    else:
257
 
        raise ValueError("method must be 'linear', 'quadratic', or "
258
 
            "'logarithmic' but a value of %r was given." % method)
259
 
 
260
 
    return cos(phase_angle + phi)
261
 
 
262
 
 
263
 
def chirp(t, f0, t1, f1, method='linear', phi=0, vertex_zero=True,
264
 
                                                            qshape=None):
 
192
def chirp(t, f0, t1, f1, method='linear', phi=0, vertex_zero=True):
265
193
    """Frequency-swept cosine generator.
266
194
 
267
195
    In the following, 'Hz' should be interpreted as 'cycles per time unit';
288
216
        This parameter is only used when `method` is 'quadratic'.
289
217
        It determines whether the vertex of the parabola that is the graph
290
218
        of the frequency is at t=0 or t=t1.
291
 
    qshape : str (deprecated)
292
 
        If `method` is `quadratic` and `qshape` is not None, chirp() will
293
 
        use scipy.signal.waveforms.old_chirp to compute the wave form.
294
 
        This parameter is deprecated, and will be removed in SciPy 0.9.
295
219
 
296
220
    Returns
297
221
    -------
351
275
        f1 must be positive, and f0 must be greater than f1.
352
276
 
353
277
    """
354
 
    if size(f0) > 1:
355
 
        # Preserve old behavior for one release cycle; this can be
356
 
        # removed in scipy 0.9.
357
 
        warnings.warn("Passing a list of polynomial coefficients in f0 to the "
358
 
                "function chirp is deprecated.  Use scipy.signal.sweep_poly.",
359
 
                DeprecationWarning)
360
 
        return old_chirp(t, f0, t1, f1, method, phi, qshape)
361
 
 
362
 
    if method in ['quadratic', 'quad', 'q'] and qshape is not None:
363
 
        # We must use the old version of the quadratic chirp.  Fortunately,
364
 
        # the old API *required* that qshape be either 'convex' or 'concave'
365
 
        # if the quadratic method was selected--`None` would raise an error.
366
 
        # So if the code reaches this point, we should use the old version.
367
 
        warnings.warn("The qshape keyword argument is deprecated.  "
368
 
                "Use vertex_zero.", DeprecationWarning)
369
 
        waveform = old_chirp(t, f0, t1, f1, method, phi, qshape)
370
 
        return waveform
371
278
 
372
279
    # 'phase' is computed in _chirp_phase, to make testing easier.
373
280
    phase = _chirp_phase(t, f0, t1, f1, method, vertex_zero)
378
285
 
379
286
def _chirp_phase(t, f0, t1, f1, method='linear', vertex_zero=True):
380
287
    """
381
 
    Calculate the phase used by chirp_phase to generate its output.  See
382
 
    chirp_phase for a description of the arguments.
 
288
    Calculate the phase used by chirp_phase to generate its output.
 
289
 
 
290
    See `chirp_phase` for a description of the arguments.
383
291
 
384
292
    """
385
293
    f0 = float(f0)
474
382
 
475
383
def _sweep_poly_phase(t, poly):
476
384
    """
477
 
    Calculate the phase used by sweep_poly to generate its output.  See
478
 
    sweep_poly for a description of the arguments.
 
385
    Calculate the phase used by sweep_poly to generate its output.
 
386
 
 
387
    See `sweep_poly` for a description of the arguments.
479
388
 
480
389
    """
481
390
    # polyint handles lists, ndarrays and instances of poly1d automatically.