Skip to content

Instantly share code, notes, and snippets.

@FilipDominec
Created February 18, 2015 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FilipDominec/e49f4c4497be543e5cc6 to your computer and use it in GitHub Desktop.
Save FilipDominec/e49f4c4497be543e5cc6 to your computer and use it in GitHub Desktop.
How does the spectrum of a frequency modulated wave differ from the well known amplitude-modulated one?
#!/usr/bin/env python
#-*- coding: utf-8 -*-
## Import common moduli
import matplotlib, sys, os, time
import matplotlib.pyplot as plt
import numpy as np
from scipy.constants import c, hbar, pi
## Use LaTeX
matplotlib.rc('text', usetex=True)
matplotlib.rc('font', size=8)
matplotlib.rc('text.latex', preamble = '\usepackage{amsmath}, \usepackage{yfonts}, \usepackage{txfonts}, \usepackage{lmodern},')
## Generating 1D data
x = np.linspace(1., 300*2*np.pi, 10000)
#y = np.sin(x*2*pi) * (1 + np.sin(x*2*pi * .05) * .2)
#y = np.sin(x*2*pi) * (1 + np.exp(1j* x*2*pi * .05) * .2)
y = np.sin(x*2*pi + (1 + np.sin(x*2*pi * .2) * 2.41/2**.5)) * np.exp(-x/300)
## 1D FFT with cropping for useful frequencies
freq = np.fft.fftfreq(len(x), d=(x[1]-x[0])) # calculate the frequency axis with proper spacing
yf = np.fft.fft(y, axis=0) / len(x) * 2*np.pi # calculate the FFT values
freq = np.fft.fftshift(freq) # ensures the frequency axis is a growing function
yf = np.fft.fftshift(yf) / np.exp(1j*2*np.pi*freq * x[0]) # dtto, and corrects the phase for the case when x[0] != 0
truncated = np.logical_and(freq>0, freq<2) # (optional) get the frequency range
(yf, freq) = map(lambda array: array[truncated], (yf, freq)) # (optional) truncate the data points
plt.plot(freq, np.abs(yf), color="#008800", label=u"$y'$", ls='-') # (optional) plot amplitude
#plt.plot(freq, np.unwrap(np.angle(yf)), color="#008800", label=u"$y'$", ls='--') # (optional) plot phase
## Simple axes
#plt.ylim((-0.1,1.1));
plt.yscale('log')
#plt.xlim((-0.1,1.1)); plt.xscale('linear')
## ==== Outputting ====
## Finish the plot + save
plt.xlabel(u"x");
plt.ylabel(u"y");
plt.grid()
plt.legend(prop={'size':10}, loc='upper right')
plt.savefig("output.png", bbox_inches='tight')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment