1
$\begingroup$

I'm trying to replicate in python the exponential chirp (here) but I cannot understand well the t and T in the formulas; here is what I wrote, but it is surely wrong, please could you help me?

timeLength = 6                        # seconds
fs = 44100
t = np.arange(0, timeLength, 1.0/fs)


# EXPCHIRP -----------------------------------------

t1 = np.arange(1, timeLength+1, 1.0/fs)
A = np.e**-t                                 
f0 = 2000.0                                     # Hz   
f1 = 10000.0
k = (f1/f0)*(1/t1) 
phi0 = np.pi / 2                                # phase, radiants.                                                 # sampling rate
s0 = A * np.sin(phi0 + 2*np.pi*f0*((k-1)/np.log(k)))      

UPDATE: Thank you, I made also another solution but it is an equivalent way to produce this; I like also your method, thank you for the explanation!!

timeLength = 6.0                        # seconds
fs = 44100
t = np.arange(0, timeLength, 1.0/fs)
ww = get_window("hamming", t.size)


# Real Sinusoid: EXPCHIRP 0 -----------------------------------------

A = np.e**-t                                    # Or np.arange(.8, .0, -.8/t.size)
f0 = 200.0                                     # Hz   
f1 = 1000.0
k = np.power((f1/f0),1/timeLength) 
phi0 = np.pi / 2                                # phase, radiants.                                                 # sampling rate
s0 = A * np.sin(phi0 + 2*np.pi*f0*((np.power(k,t)-1)/np.log(k)))       # time function, sinusoid.
$\endgroup$
1
$\begingroup$

The Python implementation looks almost exactly like the definitions in Wikipedia. One will need to define functions in order to make it work, like this:

# necessary imports:
import numpy as np

# start by defining a function that returns a sine wave with time-dependent frequency
# f_func is a function f(t, f0, k)
def chirp(t, f_func, f0, k):
    return np.sin(2*np.pi*f_func(t, f0, k)*t)

# for a linearly changing frequency, use this for f_func:
def f_linear_chirp(t, f0, k):
    return f0 + k*t

# for exponentially changing frequency, use:
def f_exp_chirp(t, f0, k):
    return f0*k**t

# for a visually useful example, take:
duration = 1 # seconds
fs = 2**13 # sampling rate
t = np.arange(0, timeLength, 1.0/fs)

f0 = 2 # Hz
f1 = 20 # HZ

# the coefficients are then given by
k_test_lin = (f1-f0)/timeLength
k_test_exp = (f1/f0)**(1/timeLength)

print(f'k_lin = {k_test_lin}, k_exp = {k_test_exp}')

# the time series for exsample signals can be created by:
linear_chirp_example = [chirp(x, f_linear_chirp, f0, k_test_lin) for x in t]
exp_chirp_example = [chirp(x, f_exp_chirp, f0, k_test_exp) for x in t]

# to look at the results:
import matplotlib.pyplot as plt
plt.plot(t, linear_chirp_example)
plt.show()
plt.plot(t, exp_chirp_example)
plt.show()

*NOTE that the instantaneous frequency at any given t is df(t)/dt!

$\endgroup$
  • $\begingroup$ Thank you; I modified the first post with an alternate way; thank you so much $\endgroup$ – BADWOLF Apr 23 at 13:39
  • 1
    $\begingroup$ Actually the instantaneous frequency is not df(t)/dt. Taking the derivative of the phase w.r.t $t$ (after dividing by $2\pi$) gives: $\frac{\partial f(t)}{\partial t}\cdot t+ f(t)$ - where the chain rule has been used. To see the problem - the initial frequency of the LFM pulse should be $f_0$, but taking the derivative of $f(t)$, the $f_0$ drops out. You will also end up with an incorrect value for the chirp rate - off by a factor of 2. $\endgroup$ – David Apr 23 at 16:14
  • $\begingroup$ Point! Important to me was that it's not just the frequency that results from evaluating f(t) at t. $\endgroup$ – HaraldCoder Apr 24 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.