Skip to content

Commit

Permalink
Added FFT stuff to day1/cheatsheet6.
Browse files Browse the repository at this point in the history
--HG--
branch : scipy2010
  • Loading branch information
Puneeth Chaganti committed Jul 28, 2010
1 parent fe196af commit 2679b58
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion day1/cheatsheet6.tex
Expand Up @@ -126,7 +126,67 @@ \section{ODE}
\begin{lstlisting}
In []: y = odeint(f, y0, t)
\end{lstlisting}
Note: To solve a system of ODEs, we need to change the function to return the right hand side of all the equations and the system and the pass the required number of initial conditions to the \typ{odeint} function.
Note: To solve a system of ODEs, we need to change the function to
return the right hand side of all the equations and the system and the
pass the required number of initial conditions to the \typ{odeint}
function.

\section{FFT}
\begin{itemize}
\item We have a simple signal $y(t)$
\item Find the FFT and plot it
\end{itemize}
\begin{lstlisting}
In []: t = linspace(0, 2*pi, 500)
In []: y = sin(4*pi*t)

In []: f = fft(y)
In []: freq = fftfreq(500, t[1] - t[0])

In []: plot(freq[:250], abs(f)[:250])
In []: grid()
\end{lstlisting}
\begin{itemize}
\item We now calculate the inverse Fourier transform.
\item Then, verify the solution obtained.
\end{itemize}
\begin{lstlisting}
In []: y1 = ifft(f) # inverse FFT
In []: allclose(y, y1)
Out[]: True
\end{lstlisting}
\begin{itemize}
\item Let us add some noise to the signal
\end{itemize}
\begin{lstlisting}
In []: yr = y + random(size=500)*0.2
In []: yn = y + normal(size=500)*0.2

In []: plot(t, yr)
In []: figure()
In []: plot(freq[:250],
...: abs(fft(yn))[:250])
\end{lstlisting}
\begin{itemize}
\item \typ{random}: produces uniform deviates in $[0, 1)$
\item \typ{normal}: draws random samples from a Gaussian
distribution
\item Useful to create a random matrix of any shape
\end{itemize}

\begin{itemize}
\item Now, we filter the noisy signal using a Wiener filter
\end{itemize}
\begin{lstlisting}
In []: from scipy import signal
In []: yc = signal.wiener(yn, 5)
In []: clf()
In []: plot(t, yc)
In []: figure()
In []: plot(freq[:250],
...: abs(fft(yc))[:250])
\end{lstlisting}

\section{Links and References}
\begin{itemize}
\item Documentation for Numpy and Scipy is available at:\\ http://docs.scipy.org/doc/
Expand Down

0 comments on commit 2679b58

Please sign in to comment.