Skip to content

Commit f84fddd

Browse files
authored
Docs: add example (#96)
1 parent 9b330c6 commit f84fddd

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ makedocs(
77
pages = Any[
88
"Home" => "index.md",
99
"API" => "fft.md",
10+
"Examples" => "examples.md",
1011
],
1112
)
1213

docs/src/examples.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Examples
2+
3+
Here you can find some basic examples of how to use this package.
4+
5+
6+
## Spectrum of a 1D Signal
7+
8+
This example shows how to obtain and plot the spectrum of a simple, real-valued signal with a second-order harmonic using FFTW and [Plots](https://github.com/JuliaPlots/Plots.jl).
9+
10+
```julia
11+
using Plots
12+
using FFTW
13+
14+
# Number of points
15+
N = 2^12 - 1
16+
# Sample spacing
17+
Ts = 1 / (1.1 * N)
18+
# Sample rate
19+
fs = 1 / Ts
20+
# Start time
21+
t0 = 0
22+
tmax = t0 + N * Ts
23+
24+
# time coordinate
25+
t = t0:Ts:tmax
26+
27+
# The underlying signal here is the sum of a sine wave at 60 cycles per second
28+
# and its second harmonic (120 cycles per second) at half amplitude. We have
29+
# discrete observations (samples) of this signal at each time `t`, with `fs`
30+
# samples per second.
31+
32+
signal = sin.(2π * 60 * t) + .5 * sin.(2π * 120 * t)
33+
34+
# The `fft` function calculates the (discrete) Fourier transform of its input.
35+
# The first half of the returned array contains the positive frequencies, while
36+
# the second half contains the negative ones. For visualization purposes, we
37+
# rearrange the array to have the zero-frequency at the center.
38+
39+
F = fftshift(fft(signal))
40+
freqs = fftshift(fftfreq(length(t), fs))
41+
42+
# Plot
43+
time_domain = plot(t, signal, title="Signal", xlims=(0, 4 / 60), xlabel="time (s)", label="")
44+
freq_domain = plot(freqs, abs.(F), title="Spectrum", xlims=(0, 200), xlabel="frequency (Hz)", label="")
45+
plot(time_domain, freq_domain, layout = 2)
46+
savefig("Wave.pdf")
47+
```
48+
49+
![](img/1D60Hz.png)

docs/src/img/1D60Hz.png

18.3 KB
Loading

0 commit comments

Comments
 (0)