锯齿波信号The sawtooth signal

lab里学到的代码和输出,说实话有些不懂但留着以后能翻查

最简单的锯齿波

from scipy import signal
fs= 8000                   # sampling frequency
t = np.arange(0, 2, 1/fs)# ...       # time vector
f0 = 200                # frequency in Hz for scipy sawtooth
saw_tooth = signal.sawtooth(2 * np.pi * f0 * t)

在这里插入图片描述

傅里叶级数和锯齿波

在这里插入图片描述

fs=8000 # sampling frequency

t=np.arange(0,2,1/fs) # time vector
f0=2                  # fundamental frequency in Hz
sin1=np.sin(2*np.pi*f0*t)
sin2=np.sin(2*np.pi*2*f0*t)/2
sin3=np.sin(2*np.pi*3*f0*t)/3
sin4=np.sin(2*np.pi*4*f0*t)/4
def generateSawTooth(f0=2, length = 2, fs=8000, order=10, height=1):
    """
    Return a saw-tooth signal with given parameters.
    
    Parameters
    ----------
    f0 : float, optional
        fundamental frequency $f_0$ of the signal to be generated,
        default: 1 Hz
    length : float, optional
        length of the signal to be generated, default: 2 sec.
    fs : float, optional
        sampling frequency $f_s$, default: 8000 Hz
    order : int, optional
        number of sinosuids to approximate saw-tooth, default: 10
    height : float, optional
        height of saw-tooth, default: 1

    Returns
    -------
    sawTooth
        generated sawtooth signal
    t
        matching time vector
    """
        
    t=np.arange(0,length,1/fs) # time vector
    sum = np.zeros(len(t))
    for ii in range(order):
        jj=ii+1
        sum += np.sin(2*np.pi*jj*f0*t)/jj
    return 2*height*sum/np.pi, t

# generate a sawtooth signal composed of 10 sinusoids
saw,t = generateSawTooth(order=10)
plt.subplot(2,1,1)
plt.plot(t,saw)
plt.xlabel('time $t$ in seconds');
plt.title('Saw-tooth signal generated from 10 sinusoids')

# generate a sawtooth signal composed of 100 sinusoids
saw,t = generateSawTooth(order=100)
plt.subplot(2,1,2)
plt.plot(t,saw)
plt.xlabel('time $t$ in seconds');
plt.title('Saw-tooth signal generated from 10 sinusoids')

plt.tight_layout() # this allowes for some space for the title text.

请添加图片描述

傅里叶级数和锯齿波(time-reverse)

在这里插入图片描述

def generateSawTooth2(f0=1, length = 2, fs=8000, order=10, height=1):
    """
    Return a saw-tooth signal with given parameters.
    
    Parameters
    ----------
    f0 : float, optional
        fundamental frequency $f_0$ of the signal to be generated,
        default: 1 Hz
    length : float, optional
        length of the signal to be generated, default: 2 sec.
    fs : float, optional
        sampling frequency $f_s$, default: 8000 Hz
    order : int, optional
        number of sinosuids to approximate saw-tooth, default: 10

    Returns
    -------
    sawTooth
        generated sawtooth signal
    t
        matching time vector
    """
    t=np.arange(0,length,1/fs)  # time vector
    sawTooth = np.zeros(len(t)) # pre-allocate variable with zeros 
    for ii in range(1,order+1):
        sign = 2*(ii % 2) - 1# create alternating sign
        sawTooth += np.sin(2*np.pi*ii*f0*t)/ii
        #print(str(ii)+': adding ' + str(sign) + ' sin(2 $\pi$ '+str(ii*f0)+' Hz t)')
    return -2*height/np.pi*sawTooth, t

f0=1
saw2,t = generateSawTooth2(f0)
plt.plot(t,saw2,label='sawtooth Fourier')
plt.ylabel('$x_{\mathrm{saw}}(t)$')
plt.xlabel('time $t$ in seconds');

# compare to the sawtooth signal generated by scipy
saw_scipy = signal.sawtooth(2 * np.pi * f0 * t)

plt.plot(t, saw_scipy, '--', label='scipy sawtooth');
plt.legend();

请添加图片描述

傅里叶级数和三角信号

在这里插入图片描述

def generateTriangular(f0=2, length = 2, fs=8000, order=10, height=1):
    """
    Return a saw-tooth signal with given parameters.
    
    Parameters
    ----------
    f0 : float, optional
        fundamental frequency $f_0$ of the signal to be generated,
        default: 1 Hz
    length : float, optional
        length of the signal to be generated, default: 2 sec.
    fs : float, optional
        sampling frequency $f_s$, default: 8000 Hz
    order : int, optional
        number of sinosuids to approximate saw-tooth, default: 10
    height : float, optional
        height of saw-tooth, default: 1

    Returns
    -------
    sawTooth
        generated sawtooth signal
    t
        matching time vector
    """
        
    t=np.arange(0,length,1/fs) # time vector
    sum = np.zeros(len(t))
    for ii in range(1, order+1, 2):
        sign = -1* (ii % 4) + 2# create alternating sign
        print(str(ii)+': adding ' + str(sign) + ' sin(2 $\pi$ '+str(ii*f0)+' Hz t) / ' + str(ii**2))
        sum += sign*np.sin(2*np.pi*ii*f0*t)/(ii**2)
    return 8*height/(np.pi**2)*sum, t

# let's use the function and generate and plot a tringular wave form
f0=2
tri,t = generateTriangular(f0,order=10)
plt.plot(t,tri)
plt.ylabel('$x_{\mathrm{tri}}(t)$');
plt.xlabel('time $t$ in seconds');

请添加图片描述

傅里叶级数和方波

在这里插入图片描述

def generateSquare(f0=1, length = 2, fs=8000, order=10):
    t=np.arange(0,length,1/fs)  # time vector
    sum = np.zeros(len(t)) # pre-allocate variable with zeros 
    for ii in range(1, order+1, 2):
        sum += np.sin(2*np.pi*ii*f0*t)/ii
        #print(str(ii)+': adding sin(2 $\pi$ '+str(ii*f0)+' Hz t)')
    return 4/np.pi*sum, t

# let's use the function and generate and plot a square wave form
f0=1 # desired frequency in Hz
rec,t = generateSquare(f0,order=20)
plt.plot(t,rec,label='square Fourier');
plt.ylabel('rectangular signal $x_{\mathrm{rect}}(t)$')
plt.xlabel('time $t$ in seconds');

# compare to the rectangular/square wave signal generated by scipy
rec_scipy = sig.square(2 * np.pi * f0 * t)
plt.plot(t,rec_scipy,'--',label='scipy square wave')
plt.legend();

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值