-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: add example #96
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ makedocs( | |
pages = Any[ | ||
"Home" => "index.md", | ||
"API" => "fft.md", | ||
"Examples" => "examples.md", | ||
], | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Examples | ||
|
||
Here you can find some basic examples of how to use this package. | ||
|
||
|
||
## Spectrum of a 1D Signal | ||
|
||
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). | ||
|
||
```julia | ||
using Plots | ||
using FFTW | ||
|
||
# Number of points | ||
N = 2^12 - 1 | ||
# Sample spacing | ||
Ts = 1 / (1.1 * N) | ||
# Sample rate | ||
fs = 1 / Ts | ||
# Start time | ||
t0 = 0 | ||
tmax = t0 + N * Ts | ||
|
||
# time coordinate | ||
t = t0:Ts:tmax | ||
|
||
# The underlying signal here is the sum of a sine wave at 60 cycles per second | ||
# and its second harmonic (120 cycles per second) at half amplitude. We have | ||
# discrete observations (samples) of this signal at each time `t`, with `fs` | ||
# samples per second. | ||
|
||
signal = sin.(2π * 60 * t) + .5 * sin.(2π * 120 * t) | ||
heitorPB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# The `fft` function calculates the (discrete) Fourier transform of its input. | ||
# The first half of the returned array contains the positive frequencies, while | ||
# the second half contains the negative ones. For visualization purposes, we | ||
# rearrange the array to have the zero-frequency at the center. | ||
|
||
F = fftshift(fft(signal)) | ||
freqs = fftshift(fftfreq(length(t), fs)) | ||
ararslan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Plot | ||
time_domain = plot(t, signal, title="Signal", xlims=(0, 4 / 60), xlabel="time (s)", label="") | ||
freq_domain = plot(freqs, abs.(F), title="Spectrum", xlims=(0, 200), xlabel="frequency (Hz)", label="") | ||
plot(time_domain, freq_domain, layout = 2) | ||
savefig("Wave.pdf") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could consider make this a Documenter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Btw, you don't have to make this change as part of this PR unless you want to. I mention it more as a note.) |
||
``` | ||
|
||
![](img/1D60Hz.png) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
together, this is equivalent to saying that the sample rate is
1.1 * N
Hz, i.e. 4504.5