test_subject
test_subject

Reputation: 57

Plot Magnitude Spectrum of Triangle Wave

I got a periodic triangle wave (pic. 1) and I need to plot its magnitude spectrum (like pic. 2) for different \tau values (\tau = T/2, \tau = T/4, \tau = T/16). Is there any way to do this in matplotlib? It seems that scipy triangle function doesn't even allow to define the triangle wave with "gaps". And magnitude_spectrum from matplotlib produces 'continuous' plot while I need 'discrete' one.

UPD: The question was previously about plotting the Fourier transform but I figured out that's probably not exactly what I wanted.

enter image description here enter image description here

Upvotes: 0

Views: 1774

Answers (1)

Cameron Chandler
Cameron Chandler

Reputation: 418

How is this?

from scipy import signal
import matplotlib.pyplot as plt
import numpy as np

def ξ(x, a, τ):
    return [a*max(0, e) for e in -signal.sawtooth(τ * np.pi * x, 0.5)]

x = np.linspace(-3, 3, 400)
plt.plot(x, ξ(x, a=2, τ=1))  
plt.xlabel('x')
plt.ylabel('ξ')
plt.title('Fourier Transform')
plt.show()

enter image description here

Upvotes: 2

Related Questions