day1/cheatsheet6.tex
branchscipy2010
changeset 438 8af5dfa5432b
parent 341 7ae88b9da553
equal deleted inserted replaced
436:7c9e949851e2 438:8af5dfa5432b
   124 \end{lstlisting}
   124 \end{lstlisting}
   125 To solve the ODE, we call the \typ{odeint} function with three arguments - the function \typ{f}, initial conditions and the time vector. 
   125 To solve the ODE, we call the \typ{odeint} function with three arguments - the function \typ{f}, initial conditions and the time vector. 
   126 \begin{lstlisting}
   126 \begin{lstlisting}
   127 In []: y = odeint(f, y0, t)
   127 In []: y = odeint(f, y0, t)
   128 \end{lstlisting}
   128 \end{lstlisting}
   129 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.
   129 Note: To solve a system of ODEs, we need to change the function to
       
   130 return the right hand side of all the equations and the system and the
       
   131 pass the required number of initial conditions to the \typ{odeint}
       
   132 function.
       
   133 
       
   134 \section{FFT}
       
   135 \begin{itemize}
       
   136     \item We have a simple signal $y(t)$
       
   137     \item Find the FFT and plot it
       
   138 \end{itemize}
       
   139 \begin{lstlisting}
       
   140 In []: t = linspace(0, 2*pi, 500)
       
   141 In []: y = sin(4*pi*t)
       
   142 
       
   143 In []: f = fft(y)
       
   144 In []: freq = fftfreq(500, t[1] - t[0])
       
   145 
       
   146 In []: plot(freq[:250], abs(f)[:250])
       
   147 In []: grid()
       
   148 \end{lstlisting} 
       
   149 \begin{itemize}
       
   150     \item We now calculate the inverse Fourier transform.
       
   151     \item Then, verify the solution obtained. 
       
   152 \end{itemize}
       
   153 \begin{lstlisting}
       
   154 In []: y1 = ifft(f) # inverse FFT
       
   155 In []: allclose(y, y1)
       
   156 Out[]: True
       
   157 \end{lstlisting} 
       
   158 \begin{itemize}
       
   159 \item Let us add some noise to the signal
       
   160 \end{itemize}
       
   161 \begin{lstlisting}
       
   162 In []: yr = y + random(size=500)*0.2
       
   163 In []: yn = y + normal(size=500)*0.2
       
   164 
       
   165 In []: plot(t, yr)
       
   166 In []: figure()
       
   167 In []: plot(freq[:250],
       
   168   ...:      abs(fft(yn))[:250])
       
   169 \end{lstlisting}
       
   170 \begin{itemize}
       
   171     \item \typ{random}: produces uniform deviates in $[0, 1)$
       
   172     \item \typ{normal}: draws random samples from a Gaussian
       
   173         distribution
       
   174     \item Useful to create a random matrix of any shape
       
   175 \end{itemize}
       
   176 
       
   177 \begin{itemize}
       
   178 \item Now, we filter the noisy signal using a Wiener filter
       
   179 \end{itemize}
       
   180 \begin{lstlisting}
       
   181 In []: from scipy import signal
       
   182 In []: yc = signal.wiener(yn, 5)
       
   183 In []: clf()
       
   184 In []: plot(t, yc)
       
   185 In []: figure()
       
   186 In []: plot(freq[:250], 
       
   187   ...:      abs(fft(yc))[:250])
       
   188 \end{lstlisting}
       
   189 
   130 \section{Links and References}
   190 \section{Links and References}
   131 \begin{itemize}
   191 \begin{itemize}
   132 \item Documentation for Numpy and Scipy is available at:\\ http://docs.scipy.org/doc/
   192 \item Documentation for Numpy and Scipy is available at:\\ http://docs.scipy.org/doc/
   133   \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \\ http://www.scipy.org/Cookbook/
   193   \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \\ http://www.scipy.org/Cookbook/
   134 \end{itemize}
   194 \end{itemize}