Sampling, clipping and aliasing
Interactive: pick a sampling frequency
The dense signal above is \(y = 3 + 3\sin(2\pi \cdot 10 t)\) — a 10 Hz tone.
Pick \(f_s\) below. Red dots are the samples; the blue line joins them, so you see
the (possibly aliased) frequency the digital system believes in. Nyquist says
\(f_s > 2f = 20\) Hz keeps the true 10 Hz.
At \(f_s = 15\) Hz: \(f_a = |10 - 15 \times \mathrm{NINT}(10/15)| = 5\) Hz — aliasing. Try 30 Hz to fix it, or 4 Hz to see the \(2\) Hz twin of the \(6\) Hz case.
if \(f_s > 2 f\) , no aliasing
if \(2/3 f < f_s < 2 f\), \(f_a = |f_s - f|\)
if \(f_s < 2/3 f\), \(f_a = (f/f_f)f_f\), where \(f_f = f_s/2\)
in most cases:
$\(f_a = \left|f- f_s \cdot NINT (f/f_s) \right|\)$
How do we use it? we use trial/error and smart guesses:
let's check different options:
if it's aliasing we could be in one of the two regions, either 6 Hz < 2/3 f, i.e. f > 1.5*6, f > 9 or
it can be that 6Hz is above 2/3f and below 2f, i.e. f is between 3 and 9 Hz
theoretically speaking if f is below 3Hz, then we're above the Nyquist frequency.
So, we see 2 Hz (2 peaks in 1 second) so it can be that we sampled correctly using 6 Hz or incorrectly and got aliasing.
Therefore we could test few things, but first let's prepare some options:
-
if we are fine and 2Hz is there, so we just see not a nice sine signal and we can increase the sampling frequency and get it nice
-
if we are aliasing, then it can be what? 2Hz = |f - 6Hz * NINT(f/6Hz)|, so f = 6Hz * NINT(f/6Hz) + 2Hz. Let's see if 10 Hz is reasonable: 10/6 = 1.6667 and the nearest integer is 2 and it means that 10 - 6*2 = 2 Hz.
-
it can be also lower than 6Hz and we got 2Hz for instance if we sampled f = 4Hz
{"var_name":"fs_choice","kind":"dropdown","values":["4 Hz","6 Hz","9 Hz","11 Hz","15 Hz","30 Hz (no aliasing)"],"default":"15 Hz","label":"Sampling frequency $f_s$"}
{"\"4 Hz\"":{"9":"\u003cdiv class=\"marimo-book-output\"\u003e\n\u003cmarimo-dropdown data-initial-value='[\u0026quot;4 Hz\u0026quot;]' data-label='\u0026quot;\u0026#92;u003cspan class=\u0026#92;\u0026quot;markdown prose dark:prose-invert contents\u0026#92;\u0026quot;\u0026#92;u003e\u0026#92;u003cspan class=\u0026#92;\u0026quot;paragraph\u0026#92;\u0026quot;\u0026#92;u003eSampling frequency \u0026#92;u003cmarimo-tex class=\u0026#92;\u0026quot;arithmatex\u0026#92;\u0026quot;\u0026#92;u003e||(f_s||)\u0026#92;u003c/marimo-tex\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026quot;' data-options='[\u0026quot;4 Hz\u0026quot;,\u0026quot;6 Hz\u0026quot;,\u0026quot;9 Hz\u0026quot;,\u0026quot;11 Hz\u0026quot;,\u0026quot;15 Hz\u0026quot;,\u0026quot;30 Hz (no aliasing)\u0026quot;]' data-allow-select-none='false' data-searchable='false' data-full-width='false' data-disabled='false'\u003e\u003c/marimo-dropdown\u003e\n\u003c/div\u003e","10":"\u003cdiv class=\"marimo-book-output\"\u003e\u003cimg src=\"../assets/img/80fd2f2482b93128c028c32dd009cdf5d1e718b3c44970ddea059d77621c9462.webp\" loading=\"lazy\" decoding=\"async\" width=\"935\" height=\"683\" /\u003e\u003c/div\u003e","11":"\u003cp\u003eAt \u003cspan class=\"arithmatex\"\u003e\\(f_s = 4\\)\u003c/span\u003e Hz: \u003cspan class=\"arithmatex\"\u003e\\(f_a = |10 - 4 \\times \\mathrm{NINT}(10/4)| = 2\\)\u003c/span\u003e Hz \u2014 aliasing. Try 30 Hz to fix it, or 4 Hz to see the \u003cspan class=\"arithmatex\"\u003e\\(2\\)\u003c/span\u003e Hz twin of the \u003cspan class=\"arithmatex\"\u003e\\(6\\)\u003c/span\u003e Hz case.\u003c/p\u003e"},"\"6 Hz\"":{"9":"\u003cdiv class=\"marimo-book-output\"\u003e\n\u003cmarimo-dropdown data-initial-value='[\u0026quot;6 Hz\u0026quot;]' data-label='\u0026quot;\u0026#92;u003cspan class=\u0026#92;\u0026quot;markdown prose dark:prose-invert contents\u0026#92;\u0026quot;\u0026#92;u003e\u0026#92;u003cspan class=\u0026#92;\u0026quot;paragraph\u0026#92;\u0026quot;\u0026#92;u003eSampling frequency \u0026#92;u003cmarimo-tex class=\u0026#92;\u0026quot;arithmatex\u0026#92;\u0026quot;\u0026#92;u003e||(f_s||)\u0026#92;u003c/marimo-tex\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026quot;' data-options='[\u0026quot;4 Hz\u0026quot;,\u0026quot;6 Hz\u0026quot;,\u0026quot;9 Hz\u0026quot;,\u0026quot;11 Hz\u0026quot;,\u0026quot;15 Hz\u0026quot;,\u0026quot;30 Hz (no aliasing)\u0026quot;]' data-allow-select-none='false' data-searchable='false' data-full-width='false' data-disabled='false'\u003e\u003c/marimo-dropdown\u003e\n\u003c/div\u003e","10":"\u003cdiv class=\"marimo-book-output\"\u003e\u003cimg src=\"../assets/img/34e52f5a399fe2ea0e8cc32355ea9cecf21ef425f83ebafca9c34c2e81fd7422.webp\" loading=\"lazy\" decoding=\"async\" width=\"935\" height=\"683\" /\u003e\u003c/div\u003e","11":"\u003cp\u003eAt \u003cspan class=\"arithmatex\"\u003e\\(f_s = 6\\)\u003c/span\u003e Hz: \u003cspan class=\"arithmatex\"\u003e\\(f_a = |10 - 6 \\times \\mathrm{NINT}(10/6)| = 2\\)\u003c/span\u003e Hz \u2014 aliasing. Try 30 Hz to fix it, or 4 Hz to see the \u003cspan class=\"arithmatex\"\u003e\\(2\\)\u003c/span\u003e Hz twin of the \u003cspan class=\"arithmatex\"\u003e\\(6\\)\u003c/span\u003e Hz case.\u003c/p\u003e"},"\"9 Hz\"":{"9":"\u003cdiv class=\"marimo-book-output\"\u003e\n\u003cmarimo-dropdown data-initial-value='[\u0026quot;9 Hz\u0026quot;]' data-label='\u0026quot;\u0026#92;u003cspan class=\u0026#92;\u0026quot;markdown prose dark:prose-invert contents\u0026#92;\u0026quot;\u0026#92;u003e\u0026#92;u003cspan class=\u0026#92;\u0026quot;paragraph\u0026#92;\u0026quot;\u0026#92;u003eSampling frequency \u0026#92;u003cmarimo-tex class=\u0026#92;\u0026quot;arithmatex\u0026#92;\u0026quot;\u0026#92;u003e||(f_s||)\u0026#92;u003c/marimo-tex\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026quot;' data-options='[\u0026quot;4 Hz\u0026quot;,\u0026quot;6 Hz\u0026quot;,\u0026quot;9 Hz\u0026quot;,\u0026quot;11 Hz\u0026quot;,\u0026quot;15 Hz\u0026quot;,\u0026quot;30 Hz (no aliasing)\u0026quot;]' data-allow-select-none='false' data-searchable='false' data-full-width='false' data-disabled='false'\u003e\u003c/marimo-dropdown\u003e\n\u003c/div\u003e","10":"\u003cdiv class=\"marimo-book-output\"\u003e\u003cimg src=\"../assets/img/9ae32bf0443b487384b62eebc20c4bac82b6a81fd61b9e60116eeb48e175be44.webp\" loading=\"lazy\" decoding=\"async\" width=\"935\" height=\"683\" /\u003e\u003c/div\u003e","11":"\u003cp\u003eAt \u003cspan class=\"arithmatex\"\u003e\\(f_s = 9\\)\u003c/span\u003e Hz: \u003cspan class=\"arithmatex\"\u003e\\(f_a = |10 - 9 \\times \\mathrm{NINT}(10/9)| = 1\\)\u003c/span\u003e Hz \u2014 aliasing. Try 30 Hz to fix it, or 4 Hz to see the \u003cspan class=\"arithmatex\"\u003e\\(2\\)\u003c/span\u003e Hz twin of the \u003cspan class=\"arithmatex\"\u003e\\(6\\)\u003c/span\u003e Hz case.\u003c/p\u003e"},"\"11 Hz\"":{"9":"\u003cdiv class=\"marimo-book-output\"\u003e\n\u003cmarimo-dropdown data-initial-value='[\u0026quot;11 Hz\u0026quot;]' data-label='\u0026quot;\u0026#92;u003cspan class=\u0026#92;\u0026quot;markdown prose dark:prose-invert contents\u0026#92;\u0026quot;\u0026#92;u003e\u0026#92;u003cspan class=\u0026#92;\u0026quot;paragraph\u0026#92;\u0026quot;\u0026#92;u003eSampling frequency \u0026#92;u003cmarimo-tex class=\u0026#92;\u0026quot;arithmatex\u0026#92;\u0026quot;\u0026#92;u003e||(f_s||)\u0026#92;u003c/marimo-tex\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026quot;' data-options='[\u0026quot;4 Hz\u0026quot;,\u0026quot;6 Hz\u0026quot;,\u0026quot;9 Hz\u0026quot;,\u0026quot;11 Hz\u0026quot;,\u0026quot;15 Hz\u0026quot;,\u0026quot;30 Hz (no aliasing)\u0026quot;]' data-allow-select-none='false' data-searchable='false' data-full-width='false' data-disabled='false'\u003e\u003c/marimo-dropdown\u003e\n\u003c/div\u003e","10":"\u003cdiv class=\"marimo-book-output\"\u003e\u003cimg src=\"../assets/img/c8bc2acbfe2449227d69af8c63654f33b26446e380dacb7f74ee22da9da232ec.webp\" loading=\"lazy\" decoding=\"async\" width=\"935\" height=\"683\" /\u003e\u003c/div\u003e","11":"\u003cp\u003eAt \u003cspan class=\"arithmatex\"\u003e\\(f_s = 11\\)\u003c/span\u003e Hz: \u003cspan class=\"arithmatex\"\u003e\\(f_a = |10 - 11 \\times \\mathrm{NINT}(10/11)| = 1\\)\u003c/span\u003e Hz \u2014 aliasing. Try 30 Hz to fix it, or 4 Hz to see the \u003cspan class=\"arithmatex\"\u003e\\(2\\)\u003c/span\u003e Hz twin of the \u003cspan class=\"arithmatex\"\u003e\\(6\\)\u003c/span\u003e Hz case.\u003c/p\u003e"},"\"30 Hz (no aliasing)\"":{"9":"\u003cdiv class=\"marimo-book-output\"\u003e\n\u003cmarimo-dropdown data-initial-value='[\u0026quot;30 Hz (no aliasing)\u0026quot;]' data-label='\u0026quot;\u0026#92;u003cspan class=\u0026#92;\u0026quot;markdown prose dark:prose-invert contents\u0026#92;\u0026quot;\u0026#92;u003e\u0026#92;u003cspan class=\u0026#92;\u0026quot;paragraph\u0026#92;\u0026quot;\u0026#92;u003eSampling frequency \u0026#92;u003cmarimo-tex class=\u0026#92;\u0026quot;arithmatex\u0026#92;\u0026quot;\u0026#92;u003e||(f_s||)\u0026#92;u003c/marimo-tex\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026#92;u003c/span\u0026#92;u003e\u0026quot;' data-options='[\u0026quot;4 Hz\u0026quot;,\u0026quot;6 Hz\u0026quot;,\u0026quot;9 Hz\u0026quot;,\u0026quot;11 Hz\u0026quot;,\u0026quot;15 Hz\u0026quot;,\u0026quot;30 Hz (no aliasing)\u0026quot;]' data-allow-select-none='false' data-searchable='false' data-full-width='false' data-disabled='false'\u003e\u003c/marimo-dropdown\u003e\n\u003c/div\u003e","10":"\u003cdiv class=\"marimo-book-output\"\u003e\u003cimg src=\"../assets/img/a170e2c44252b07d7e5613a94b7a3c65b9b81e60f14e11f2001c29b4032756d9.webp\" loading=\"lazy\" decoding=\"async\" width=\"935\" height=\"683\" /\u003e\u003c/div\u003e","11":"\u003cp\u003eAt \u003cspan class=\"arithmatex\"\u003e\\(f_s = 30\\)\u003c/span\u003e Hz you are above Nyquist (\u003cspan class=\"arithmatex\"\u003e\\(\u0026gt; 20\\)\u003c/span\u003e Hz): \u003cspan class=\"arithmatex\"\u003e\\(f_a = 10\\)\u003c/span\u003e Hz \u2014 the true tone survives.\u003c/p\u003e"}}