From d0a2291aa87fbb4472f2862b47444302e26f8878 Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Mon, 13 Aug 2018 14:30:06 +0200 Subject: [PATCH] Update README and example --- README.md | 32 +++++-------- examples/FIR_design.jmd | 99 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 22 deletions(-) create mode 100644 examples/FIR_design.jmd diff --git a/README.md b/README.md index 8ef5630..a23a8e6 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,19 @@ and Sweave. You can write your documentation and code in input document using Noweb, Markdown, Script syntax and use `weave` function to execute to document to capture results -and figures. +and figures. **Weave will support Julia 1.0 as soon as all dependencies are up to date, use 0.7 for now** **Current features** * Noweb, markdown or script syntax for input documents. * Execute code as terminal or "script" chunks. -* Capture Plots, Gadfly and PyPlot figures. +* Capture Plots.jl figures *(or Gadfly and PyPlot on julia 0.6)*. * Supports LaTex, Pandoc, Github markdown, MultiMarkdown, Asciidoc and reStructuredText output * Publish markdown directly to html and pdf using Julia or Pandoc markdown. * Simple caching of results * Convert to and from IJulia notebooks + **Citing Weave:** *Pastell, Matti. 2017. Weave.jl: Scientific Reports Using Julia. The Journal of Open Source Software. http://dx.doi.org/10.21105/joss.00204* ![Weave code and output](http://mpastell.com/images/weave_demo.png) @@ -33,26 +34,27 @@ and figures. You can install the latest release using Julia package manager: ```julia +using Pkg Pkg.add("Weave") ``` ## Usage -Run from julia using Gadfly for plots: +Run from julia using Plots.jl for plots: ```julia #First add depencies for the example -Pkg.add.(["Cairo", "Fontconfig", "Gadfly"]) +using Pkg; Pkg.add.(["Plots", "DSP"]) #Use Weave using Weave -weave(Pkg.dir("Weave","examples","gadfly_sample.mdw")) +weave(joinpath(dirname(pathof(Weave)), "../examples", "FIR_design.jmd"), out_path=:pwd) ``` -If you have Pandoc installed you can also weave directly to html and pdf. +If you have LaTeX installed you can also weave directly to pdf. ```julia -weave(Pkg.dir("Weave","examples","gadfly_md_sample.jmd"), informat="markdown", - out_path = :pwd, doctype = "md2html") +weave(joinpath(dirname(pathof(Weave)), "../examples", "FIR_design.jmd"), + out_path=:pwd, doctype="md2pdf") ``` ## Documentation @@ -67,20 +69,6 @@ Documenter.jl with MKDocs generated documentation: I have made [language-weave](https://atom.io/packages/language-weave) package for Atom to do the syntax highlighting correctly. -Noweb documents work well with ESS as well, to set doc-mode for .mdw files to markdown -and code to Julia you can do: - -```clojure -(defun mdw-mode () - (ess-noweb-mode) - (setq ess-noweb-default-code-mode 'ess-julia-mode) - (setq ess-noweb-doc-mode 'markdown-mode)) - -(setq auto-mode-alist (append (list (cons "\\.mdw$" 'mdw-mode)) - auto-mode-alist)) -``` - - ## Contributing I will probably add new features to Weave when I need them myself or if they are requested and not too difficult to implement. You can contribute by opening issues on Github or implementing things yourself and making a pull request. I'd also appreciate example documents written using Weave to add to examples. diff --git a/examples/FIR_design.jmd b/examples/FIR_design.jmd new file mode 100644 index 0000000..2266480 --- /dev/null +++ b/examples/FIR_design.jmd @@ -0,0 +1,99 @@ +--- +title: FIR filter design with Julia +author: Matti Pastell +date: 21th April 2016 +--- + + +# Introduction + + +This an example of a julia script that can be published using +[Weave](http://mpastell.github.io/Weave.jl/latest/usage/). +The script can be executed normally using Julia +or published to HTML or pdf with Weave. +Text is written in markdown in lines starting with "`#'` " and code +is executed and results are included in the published document. + + +Notice that you don't need to define chunk options, but you can using +`#+`. just before code e.g. `#+ term=True, caption='Fancy plots.'`. +If you're viewing the published version have a look at the +[source](FIR_design_plots.jl) to see the markup. + + + +# FIR Filter Design + + +We'll implement lowpass, highpass and ' bandpass FIR filters. If +you want to read more about DSP I highly recommend [The Scientist +and Engineer's Guide to Digital Signal +Processing](http://www.dspguide.com/) which is freely available +online. + + +## Calculating frequency response + + +DSP.jl package doesn't (yet) have a method to calculate the +the frequency response of a FIR filter so we define it: + +```julia +using Plots, DSP +gr() + +function FIRfreqz(b::Array, w = range(0, stop=π, length=1024)) + n = length(w) + h = Array{ComplexF32}(undef, n) + sw = 0 + for i = 1:n + for j = 1:length(b) + sw += b[j]*exp(-im*w[i])^-j + end + h[i] = sw + sw = 0 + end + return h +end +``` + +## Design Lowpass FIR filter + + +Designing a lowpass FIR filter is very simple to do with DSP.jl, all you +need to do is to define the window length, cut off frequency and the +window. We will define a lowpass filter with cut off frequency at 5Hz for a signal +sampled at 20 Hz. +We will use the Hamming window, which is defined as: +$w(n) = \alpha - \beta\cos\frac{2\pi n}{N-1}$, where $\alpha=0.54$ and $\beta=0.46$ + +```julia +fs = 20 +f = digitalfilter(Lowpass(5, fs = fs), FIRWindow(hamming(61))) +w = range(0, stop=pi, length=1024) +h = FIRfreqz(f, w) +``` + +## Plot the frequency and impulse response + + +The next code chunk is executed in term mode, see the [script](FIR_design.jl) for syntax. + +```julia; term=true +h_db = log10.(abs.(h)); +ws = w/pi*(fs/2) +``` + +```julia +plot(ws, h_db, + xlabel = "Frequency (Hz)", ylabel = "Magnitude (db)") +``` + +And again with default options + +```julia +h_phase = unwrap(-atan.(imag.(h),real.(h))) +plot(ws, h_phase, + xlabel = "Frequency (Hz)", ylabel = "Phase (radians)") +```