From: <fer...@us...> - 2007-12-04 18:37:52
|
Revision: 4581 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4581&view=rev Author: fer_perez Date: 2007-12-04 10:37:40 -0800 (Tue, 04 Dec 2007) Log Message: ----------- Updates I forgot to commit yesterday... Modified Paths: -------------- trunk/py4science/workbook/fft_imdenoise.tex trunk/py4science/workbook/quad_newton.tex Added Paths: ----------- trunk/py4science/examples/stock_demo.py Added: trunk/py4science/examples/stock_demo.py =================================================================== --- trunk/py4science/examples/stock_demo.py (rev 0) +++ trunk/py4science/examples/stock_demo.py 2007-12-04 18:37:40 UTC (rev 4581) @@ -0,0 +1,42 @@ +"""Simple data access and manipulation demo, using Yahoo Finance data.""" + +# <demo> stop + +# Needed libraries +import urllib +import matplotlib.mlab as mlab # contains csv2rec +import pylab as p + +# <demo> stop + +# Choose a stock, make the URL for it and download it to a local file +ticker = 'CROX' # Boulder-based company Crocs + +url = 'http://ichart.finance.yahoo.com/table.csv?' +\ + 's=%s&d=9&e=20&f=2007&g=d&a=0&b=29&c=1993&ignore=.csv'%ticker + +fname = '%s.csv'% ticker +urllib.urlretrieve(url, fname) + +# <demo> stop + +# Now, make a Record Array out of this dataset: +r = mlab.csv2rec(fname) + +# note that the CSV file is sorted most recent date first, so you will probably +# want to sort the record array so most recent date is last +r.sort() +# A quick look at the data structure: +print 'dtype:',r.dtype +print 'shape:',r.shape + +# <demo> stop + +p.plot(r.date,r.adj_close) +p.show() + +# <demo> stop +# Now, make a slightly modified version of the file with cleaner formatting. +# We'll use this later... +mlab.rec2csv(r,'dap/myserver/data/sample.csv', + formatd={'date':mlab.FormatString()}) Modified: trunk/py4science/workbook/fft_imdenoise.tex =================================================================== --- trunk/py4science/workbook/fft_imdenoise.tex 2007-12-04 17:39:31 UTC (rev 4580) +++ trunk/py4science/workbook/fft_imdenoise.tex 2007-12-04 18:37:40 UTC (rev 4581) @@ -1,50 +1,52 @@ \section{FFT Image Denoising} \label{sec:fft_imdenoise} -Convolution of an input with with a linear filter in the termporal or -spatial domain is equivalent to multiplication by the fourier -transforms of the input and the filter in the spectral domain. This -provides a conceptually simple way to think about filtering: transform -your signal into the frequency domain, dampen the frequencies you are -not interested in by multiplying the frequency spectrum by the desired -weights, and then inverse transform the multiplies spectrum back into -the original domain. In the example below, we will simply set the -weights of the frequencies we are uninterested in (the high frequency -noise) to zero rather than dampening them with a smoothly varying -function. Although this is not usually the best thing to do, since -sharp edges in one domain usually introduce artifacts in another (eg -high frequency ``ringing''), it is easy to do and sometimes provides -satisfactory results. +Convolution of an input with with a linear filter in the termporal or spatial +domain is equivalent to multiplication by the fourier transforms of the input +and the filter in the spectral domain. This provides a conceptually simple way +to think about filtering: transform your signal into the frequency domain, +dampen the frequencies you are not interested in by multiplying the frequency +spectrum by the desired weights, and then apply the inverse transform to the +modified spectrum, back into the original domain. In the example below, we +will simply set the weights of the frequencies we are uninterested in (the high +frequency noise) to zero rather than dampening them with a smoothly varying +function. Although this is not usually the best thing to do, since sharp edges +in one domain usually introduce artifacts in another (eg high frequency +``ringing''), it is easy to do and sometimes provides satisfactory results. -The image in the upper left panel of Figure~\ref{fig:fft_imdenoise} is -a grayscale photo of the moon landing. There is a banded pattern of -high frequency noise polluting the image. In the upper right panel we -see the 2D spatial frequency spectrum. The FFT output in -\texttt{scipy} is packed with the lower freqeuencies starting in the -upper left, and proceeding to higher frequencies as one moves to the -center of the spectrum (this is the most efficient way numerically to -fill the output of the FFT algorithm). Because the input signal is -real, the output spectrum is complex and symmetrical: the -transformation values beyond the midpoint of the frequency spectrum -(the Nyquist frequency) correspond to the values for negative -frequencies and are simply the mirror image of the positive -frequencies below the Nyquist (this is true for the 1D, 2D and ND FFTs -in \texttt{numpy}). +The image in the upper left panel of Figure~\ref{fig:fft_imdenoise} is a +grayscale photo of the moon landing. There is a banded pattern of high +frequency noise polluting the image. In the upper right panel we see the 2D +spatial frequency spectrum. The FFT output in \texttt{scipy} is packed with +the lower freqeuencies starting in the upper left, and proceeding to higher +frequencies as one moves to the center of the spectrum (this is the most +efficient way numerically to fill the output of the FFT algorithm). Because +the input signal is real, the output spectrum is complex and symmetrical: the +transformation values beyond the midpoint of the frequency spectrum (the +Nyquist frequency) correspond to the values for negative frequencies and are +simply the mirror image of the positive frequencies below the Nyquist (this is +true for the 1D, 2D and ND FFTs in \texttt{numpy}). -In this exercise we will compute the 2D spatial frequency spectra of -the luminance image, zero out the high frequency components, and -inverse transform back into the time domain. We can plot the input -and output images with the \texttt{pylab.imshow} function, but the -images must first be scaled to be withing the 0..1 luminance range. -For best results, it helps to \textit{amplify} the image by some scale -factor, and then \textit{clip} it to set all values greater than one -to one. This serves to enhance contrast among the darker elements of -the image, so it is not completely dominated by the brighter segments +In this exercise we will compute the 2D spatial frequency spectra of the +luminance image, zero out the high frequency components, and inverse transform +back into the spatial domain. We can plot the input and output images with the +\texttt{pylab.imshow} function, but the images must first be scaled to be +within the 0..1 luminance range. For best results, it helps to +\textit{amplify} the image by some scale factor, and then \textit{clip} it to +set all values greater than one to one. This serves to enhance contrast among +the darker elements of the image, so it is not completely dominated by the +brighter segments \lstinputlisting[label=code:fft_imdenoise,caption={IGNORED}]{problems/fft_imdenoise.py} \begin{figure} -\begin{centering}\includegraphics[width=4in]{fig/fft_imdenoise}\par\end{centering} + \begin{centering} \includegraphics[width=4in]{fig/fft_imdenoise} \par + \end{centering} -\caption{\label{fig:fft_imdenoise}High freqeuency noise filtering of a 2D image in the Fourier domain. The upper panels show the original image (left) and spectral power (right) and the lower panels show the same data with the high frequency power set to zero. Although the input and output images are grayscale, you can provide colormaps to \texttt{pylab.imshow} to plot them in psudo-color} + \caption{\label{fig:fft_imdenoise}High freqeuency noise filtering of a 2D + image in the Fourier domain. The upper panels show the original image + (left) and spectral power (right) and the lower panels show the same data + with the high frequency power set to zero. Although the input and output + images are grayscale, you can provide colormaps to \texttt{pylab.imshow} to + plot them in psudo-color} \end{figure} Modified: trunk/py4science/workbook/quad_newton.tex =================================================================== --- trunk/py4science/workbook/quad_newton.tex 2007-12-04 17:39:31 UTC (rev 4580) +++ trunk/py4science/workbook/quad_newton.tex 2007-12-04 18:37:40 UTC (rev 4581) @@ -1,34 +1,35 @@ \section{Newton's method} \label{sec:quad_newton} -Consider the problem of solving for $t$ in\begin{equation} -\int_{o}^{t}f(s)ds=u\end{equation} - where $f(s)$ is a monotonically increasing function of $s$ and -$u>0$. +Consider the problem of solving for $t$ in +\begin{equation} + \int_{o}^{t}f(s)ds=u +\end{equation} +where $f(s)$ is a monotonically increasing function of $s$ and $u>0$. -This problem can be simply solved if seen as a root finding question. -Let\begin{equation} -g(t)=\int_{o}^{t}f(s)ds-u,\end{equation} +This problem can be simply solved if seen as a root finding question. Let +\begin{equation} +g(t)=\int_{o}^{t}f(s)ds-u, +\end{equation} then we just need to find the root for $g(t),$ which is guaranteed to be unique given the conditions above. The SciPy library includes an optimization package that contains a Newton-Raphson solver called \texttt{scipy.optimize.newton.} This solver can optionally take a known derivative for the function whose -roots are being sought, and in this case the derivative is simply -\begin{equation} -\frac{dg(t)}{dt}=f(t).\end{equation} +roots are being sought, and in this case the derivative can be trivially +computed in exact form. +For this exercise, implement the solution for the test function +\[ +f(t)=t\sin^{2}(t), +\] +using +\[ +u=\frac{1}{4}. +\] -For this exercise, implement the solution for the test function\[ -f(t)=t\sin^{2}(t),\] - using \[ -u=\frac{1}{4}.\] - - The listing~\ref{code:quad_newton} contains a skeleton that includes for comparison the correct numerical value. \lstinputlisting[label=code:quad_newton,caption={IGNORED}]{problems/quad_newton.py} - - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |