├── dsp-cookbook.bib ├── dsp-cookbook.pdf ├── dsp-cookbook.tex ├── makefile ├── readme.md └── sections └── reverb.tex /dsp-cookbook.bib: -------------------------------------------------------------------------------- 1 | @book{Zolzer:2011, 2 | author = {Zolzer, Udo}, 3 | title = {DAFX: Digital Audio Effects}, 4 | year = {2011}, 5 | isbn = {0470665998, 9780470665992}, 6 | edition = {2nd}, 7 | publisher = {Wiley Publishing}, 8 | } 9 | 10 | @Inbook{Gardner:2002, 11 | author="Gardner, William G.", 12 | editor="Kahrs, Mark 13 | and Brandenburg, Karlheinz", 14 | title="Reverberation Algorithms", 15 | bookTitle="Applications of Digital Signal Processing to Audio and Acoustics", 16 | year="2002", 17 | publisher="Springer US", 18 | address="Boston, MA", 19 | pages="85--131", 20 | abstract="This chapter discusses reverberation algorithms, with emphasis on algorithms that can be implemented for realtime performance. The chapter begins with a concise framework describing the physics and perception of reverberation. This includes a discussion of geometrical, modal, and statistical models for reverberation, the perceptual effects of reverberation, and subjective and objective measures of reverberation. Algorithms for simulating early reverberation are discussed first, followed by a discussion of algorithms that simulate late, diffuse reverberation. This latter material is presented in chronological order, starting with reverberators based on comb and allpass filters, then discussing allpass feedback loops, and proceeding to recent designs based on inserting absorptive losses into a lossless prototype implemented using feedback delay networks or digital waveguide networks.", 21 | isbn="978-0-306-47042-4", 22 | doi="10.1007/0-306-47042-X_3", 23 | url="https://doi.org/10.1007/0-306-47042-X_3" 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /dsp-cookbook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PelleJuul/dsp-cookbook/f2dd1689f6a2b1d63d3eaf1ebec5c926dde10823/dsp-cookbook.pdf -------------------------------------------------------------------------------- /dsp-cookbook.tex: -------------------------------------------------------------------------------- 1 | \documentclass[11pt]{article} 2 | \usepackage{amsthm} 3 | \usepackage{amsmath} 4 | \usepackage{bm} 5 | \usepackage{makeidx} 6 | \usepackage{tikz} 7 | \usepackage{physics} 8 | \usepackage{cite} 9 | \usepackage{hyperref} 10 | \usepackage{graphicx} 11 | \usepackage{subcaption} 12 | \hypersetup{ 13 | colorlinks, 14 | citecolor=black, 15 | filecolor=black, 16 | linkcolor=black, 17 | urlcolor=black 18 | } 19 | \usetikzlibrary{arrows.meta} 20 | \let\originalleft\left 21 | \let\originalright\right 22 | \renewcommand{\left}{\mathopen{}\mathclose\bgroup\originalleft} 23 | \renewcommand{\right}{\aftergroup\egroup\originalright} 24 | 25 | %\usepackage[ 26 | % top=1.25in, 27 | % bottom=1.25in, 28 | % left=1.25in, 29 | % right=1.25in, 30 | % bindingoffset=0.25in, 31 | % heightrounded, 32 | %]{geometry} 33 | 34 | \def\lsqb{\left[} 35 | \def\rsqb{\right]} 36 | \def\sqb#1{\lsqb #1 \rsqb} 37 | \def\xsig{x\sqb{n}} 38 | \def\ysig{y\sqb{n}} 39 | 40 | \makeindex 41 | \begin{document} 42 | \title{The DSP Cookbook} 43 | \date{October 2017} 44 | \author{Pelle Juul Christensen} 45 | \maketitle 46 | \tableofcontents 47 | 48 | \section{Theory} 49 | \subsection{Sine Wave} 50 | \index{Sine waves} 51 | \index{Sin} 52 | \index{Cos} 53 | 54 | The general equation of is 55 | \begin{equation} 56 | A\sin(2\pi f t + \theta) 57 | \end{equation} 58 | where 59 | \begin{description} 60 | \item[$A$] is the amplitude of the wave. 61 | \item[$\sin$] is the $\sin$ function. 62 | \item[$2\pi$] normalizes the period of the $\sin$ function to $1$. 63 | \item[$t$] is time. 64 | \item[$f$] is the frequency of the wave. 65 | \item[$\theta$] is the initial phase of the wave. 66 | \end{description} 67 | 68 | The $\sin$ and $\cos$ functions are defined using the unit circle. Given a variable $t$ we go $t$ radians along the unit circle \footnote{A unit circle is simply a circle with a radius of 1.}. The point at $t$ radians will have coordinates $(\cos(t), \sin(t))$ --- $\cos(t)$ is the projection of the point onto the x-axis, $\sin(t)$ is the projection onto the y-axis. 69 | 70 | \begin{figure}[h] 71 | \centering 72 | \begin{tikzpicture}[scale=2, domain=0:3] 73 | \draw (0, 0) circle [radius=1.0]; 74 | \draw [->] (-1.1, 0) -- (1.1, 0) node [right] {$x$} node[pos=0.75, below] {$r=1$}; 75 | \draw [->] (0, -1.1) -- (0, 1.1) node [above] {$y$}; 76 | \draw[thick] (0, 0) -- (60:1) node[pos=1.1, right] {$(\cos(t), \sin(t))$}; 77 | \filldraw (60:1) circle [radius=0.05]; 78 | \draw[thick] [->] (1, 0) arc (0:57:1) node[pos=0.5,right] {$t$ rads}; 79 | \end{tikzpicture} 80 | \end{figure} 81 | 82 | \index{period} 83 | Sinusoids have a period of $2 \pi$ (a full arc), thus if we multiply $t$ by $2\pi$ to get $\sin(2 \pi t)$, we will get a period of $1$. If $t$ is time in seconds, we will have one period per second. 84 | 85 | To get more or less periods per second we multiply $t$ by that amount. For example $\sin(2 \pi 440 t)$ has 440 periods per second. 86 | 87 | \index{Frequency} 88 | \index{Hertz} 89 | \index{Hz|see {Hertz}} 90 | Periods per second is measured in $Hertz$ which has the unit $1/s$, which is also called the frequency of a wave. To calculate the period of a sine wave at a specific frequency $f$, you use $1/f$, e.g. a sine wave at $440\ Hz$ will have a period of $1/f=0.002\ s$. 91 | 92 | \index{Phase} 93 | We can change the starting point of a wave by adding an initial phase. A $\sin$ wave with a phase of $\pi/2$ is equivalent to a $\cos$ wave. The initial phase is usually indicated by $\theta$. 94 | 95 | \index{Amplitude} 96 | A sine wave swings between $-1$ and $1$. To increase or decrease the amplitude, we multiply with a gain factor $A$. 97 | 98 | \subsection{Phasors} 99 | \index{Phasor} 100 | A \textit{Phasor} is a complex sinusoid defined my Euler's equation 101 | \begin{equation} 102 | e^{jx} = \cos(x) + j\sin(x) 103 | \end{equation} 104 | such that we have 105 | \begin{align} 106 | \cos(x) = \Re{e^{jx}}\\ 107 | \sin(x) = \Im{e^{jx}} 108 | \end{align} 109 | 110 | \subsection{Digital Signals} 111 | \index{Digital signals} 112 | \index{Sampling rate} 113 | \index{Sampling period} 114 | Unlike real-world \textit{analog} signals, which are continuous, digital signals are represented by discrete numbers at specific points in time -- a list of numbers. Each number is separated by some amount of time called the \textit{sampling period}. 115 | 116 | If we wish to convert a analog signal to a digital one, we must sample it. This is done by measuring the analog signal one time each \textit{sampling period} ($1/samplerate$). Given the analog signal $x(t)$ then we get the digital signal 117 | 118 | \begin{equation} 119 | \xsig = x(nT) 120 | \end{equation} 121 | 122 | where $T$ is the sampling period and $n$ is the \textit{sample index}. Notice that it is convention to use square brackets for discrete signals and parentheses for continuous signals. 123 | 124 | \index{ADC} 125 | \index{Quantization} 126 | If we are measuring a real-world signal, we will use an \textit{Analog-to-Digital Converter (ADC)}. Since computers only have a finite amount of memory, we will need to choose how precise our measurements will be. If we represent out sample with $b$ bits then we will have $2^b$ levels to work with. The process of rounding our sample to the nearest representable number is called \textit{quantization}. 127 | 128 | \subsection{DSP Systems} 129 | \index{System} 130 | A DSP system (or just ''a system'') is a computer program -- an algorithm -- which takes one (or more) digital signals and returns one (or more) digital signals. 131 | 132 | \subsection{Causal Systems} 133 | In systems with memory, we need to agree on what should happen when $n < 0$. Usually we will use $x\left[n\right] = 0 \text{ for } n < 0$. Such a system is said to be \textit{causal}. 134 | 135 | If we have a system where $x\left[n\right] \neq 0 \text{ for } n < 0$, then the system is said to be \textit{non-causal}. 136 | 137 | \subsection{Difference Equations} 138 | The general form of a difference equation is 139 | 140 | \begin{equation} 141 | \begin{split} 142 | y\sqb{n} = a_1 y\sqb{n - 1} + a_2 y\sqb{n-2} + \dots + a_N y\sqb{n-N} + \\ 143 | b_0 x\sqb{n} + b_1 x\sqb{n - 1} + b_2 x\sqb{n - 2} + \dots + b_L\sqb{n - L} 144 | \end{split} 145 | \end{equation} 146 | 147 | Also written as 148 | 149 | \begin{equation} 150 | y\sqb{n} = \sum_{k=1}^N a_k y\sqb{n - k} + \sum_{k=0}^L b_k x\sqb{n - k} 151 | \end{equation} 152 | 153 | Where the $y\sqb{\cdot}$ components determine the recursive (feedback) characteristic, and the $x\sqb{\cdot}$ components the non-recursive characteristic. 154 | 155 | \subsection{Linear Time-Invariant System} 156 | \index{Linear Time-Invariant System} 157 | \index{LTI|see {Linear Time-Invariant System}} 158 | 159 | A system $T(x\sqb{n})$ is \textit{linear time-invariant} (LTI) if it satisfies both linearity and time-invariancy properties. 160 | 161 | A system is linear if it supports the scalability property \index{Linearity property} 162 | \begin{equation} 163 | T(\alpha \xsig) = \alpha T(\xsig). 164 | \end{equation} 165 | As well as the superposition property \index{Superposition property} 166 | \begin{gather} 167 | T(x_3\sqb{n}) = T(x_1\sqb{n} + x_2\sqb{n}) = T(x_1\sqb{n}) + T(x_2\sqb{n}) 168 | \end{gather} 169 | where $x_3\sqb{n} = x_1\sqb{n} + x_2\sqb{n}$. 170 | 171 | A system is time-invariant if it meets the condition 172 | \index{Time-invariant} 173 | \begin{equation} 174 | x\sqb{n - L} = y\sqb{n - L} 175 | \end{equation} 176 | for some delay $L$ --- a delay in the input signal will cause a corresponding delay in the output signal. 177 | 178 | A system is LTI if all of its subcomponents are LTIs. 179 | 180 | \subsection{Impulse Response} 181 | \index{Impulse response} 182 | An impulse is a short spike in amplitude. The ideal impulse signal is defined by 183 | 184 | \begin{equation} 185 | \delta\sqb{n} = 186 | \begin{cases} 187 | 1, & \text{if } n = 0 \\ 188 | 0, & \text{otherwise} 189 | \end{cases} 190 | \end{equation} 191 | 192 | The \textit{impulse response} of a system is defined as the system output for an input of $\xsig = \delta\sqb{n}$. In this 193 | case the output of the system is normally denoted $h\sqb{n}$. 194 | 195 | LTI systems can be described completely by their impulse responses. To obtain the output of a system based on an impulse response, one should convolve the input signal with the impulse response 196 | \begin{equation} 197 | T(\xsig) = \xsig * h_T\sqb{n} 198 | \end{equation} 199 | 200 | \index{Finite impulse repsonse} 201 | \index{FIR|see {Finite impulse repsonse}} 202 | A system has a \textit{finite impulse response} (FIR) if it does not contain any recursive components. In that case the impulse response is determined by 203 | \begin{equation} 204 | h\sqb{n} = T(\delta\sqb{n}) = \sum_{k = 0}^L b_k \delta\sqb{n - k} 205 | \end{equation} 206 | The impulse response of FIR systems will always converge to zero. 207 | 208 | \index{Infinte impulse response} 209 | \index{IIR|see {Infinite impulse response}} 210 | A system has an \textit{infinite impulse reponse} (IIR) if it contains recursive components. In that case the impulse response is determined by 211 | \begin{equation} 212 | h\sqb{n} = T(\delta\sqb{n}) = \sum_{k=1}^N a_k h\sqb{n - k} + \sum_{k = 0}^L b_k \delta\sqb{n - k} 213 | \end{equation} 214 | \index{Stability} 215 | \index{Stable} 216 | \index{Unstable} 217 | Some IIR systems will converge to a state of near-zero. Such systems are said to be \textit{stable}. IIR systems can also be \textit{unstable}, which means that their output will be divergent (i.e. will blow up to infinity). 218 | 219 | \subsection{Convolution} 220 | \index{Convolution} 221 | The \textit{convolution} of two signals $\xsig$ and $h\sqb{n}$ is defined as 222 | \begin{equation} 223 | \xsig * h\sqb{n} = \sum_{m = -\infty}^{m = \infty} x\sqb{n - m}h\sqb{m} 224 | \end{equation} 225 | 226 | In practice, $m$ will be bounded by the sizes of the signals. If $\xsig$ has size $N$ and $h\sqb{n}$ size $M$, then $m$ should be within $0$ and $M$, since no samples is defined before $0$, and no samples in $h\sqb{x}$ are defined after $M$. The equation is then 227 | \begin{equation} 228 | \xsig * h\sqb{n} = \sum_{m = 0}^{M} x\sqb{n - m}h\sqb{m} 229 | \end{equation} 230 | The result will be defined for $n$ in $0 \leq n < N + M - 1$. 231 | 232 | Convolution is commutative, meaning that 233 | \begin{equation} 234 | \xsig * h\sqb{n} = h\sqb{n} * \xsig 235 | \end{equation} 236 | It is also associative, which means that 237 | \begin{equation} 238 | g\sqb{x} * (\xsig * h\sqb{n}) = (g\sqb{x} * \xsig) * h\sqb{n} 239 | \end{equation} 240 | as well as distributive 241 | \begin{equation} 242 | g\sqb{x} * (\xsig + h\sqb{n}) = (g\sqb{x} * \xsig) + (g\sqb{x} * h\sqb{n}) 243 | \end{equation} 244 | 245 | \subsection{Digital Frequency} 246 | \index{Digital frequency} 247 | Digital frequency is defined as 248 | \begin{equation} 249 | \Omega = 2\pi f T, 250 | \end{equation} 251 | where $f$ is the analog frequency in $Hz$ and $T$ is the sampling period. This means that $\Omega=0$ denotes DC, $\Omega=\pi$ the Nyquist frequency ($1/2 * samplerate$), and $\Omega=2\pi$ the sampling frequency. 252 | 253 | \subsection{Frequency Response} 254 | \index{Frequency response} 255 | \index{Bode diagram} 256 | The frequency response of a system describes the frequency content of its impulse response in terms of magnitude and phase. The frequency response of a system is usually shown as a plot with frequency on the x-axis and amplitude on the y-axis. If the plot is presented with a logarithmic frequency scale, it is sometimes called a \textit{Bode} diagram. 257 | 258 | Using the equation 259 | \def\hfun{H(e^{j\theta})} 260 | \begin{equation} 261 | \hfun = \frac{\sum_{k=0}^{L} b_k e^{-j\theta k}}{\sum_{k=1}^{N} a_k e^{-j\theta k}} 262 | \end{equation} 263 | we get a complex number which describes the phase and magnitude response for a signal at the digital frequency $\theta$. The magnitude $\|\hfun\|$ is the gain at that frequency. The angle $\angle \hfun$ is the \textit{phase shift} at that frequency. 264 | 265 | The $\hfun$ term is derived by first selecting the input signal 266 | \begin{equation} 267 | \xsig = e^{j\theta n} 268 | \end{equation} 269 | and then convolving with the impulse response of our system 270 | \def\convsum{\sum_{m=-\infty}^{\infty}} 271 | \begin{align} 272 | \ysig &= \convsum h\sqb{m} x\sqb{n-m} = \convsum h\sqb{m} e^{j\theta(n-m)} \\ 273 | &= e^{j\theta(n)} \convsum h\sqb{m} e^{j\theta m} = \xsig \convsum h\sqb{m} e^{j\theta m} \\ 274 | &= \xsig \hfun 275 | \end{align} 276 | so we have 277 | \begin{equation} 278 | \hfun = \convsum h\sqb{m} e^{j\theta m} 279 | \end{equation} 280 | and since 281 | \begin{equation} 282 | \ysig = \xsig \hfun 283 | \end{equation} 284 | we have 285 | \begin{equation} 286 | \hfun = \frac{\ysig}{\xsig} 287 | \end{equation} 288 | 289 | To get to our final definition of $\hfun$ we first not that 290 | \begin{align} 291 | \ysig &= \convsum h\sqb{m} x\sqb{n-m} \\ 292 | \ysig &= \xsig \hfun = e^{j\theta n} \hfun 293 | \end{align} 294 | And then rearranging 295 | \begin{align} 296 | e^{j\theta n} \hfun &= \sum_{k=1}^{N} a_k e^{j\theta (n-k)} \hfun + \sum_{k=0}^{L} b_k e^{j\theta(n-k)} \\ 297 | e^{j\theta n} \hfun &= e^{j\theta n} \left( \sum_{k=1}^{N} a_k e^{-j\theta k} \hfun + \sum_{k=0}^{L} b_k e^{-j\theta k} \right)\\ 298 | \hfun &= \sum_{k=1}^{N} a_k e^{-j\theta k} \hfun + \sum_{k=0}^{L} b_k e^{-j\theta k} \\ 299 | 1 &= \sum_{k=1}^{n} a_k e^{-j\theta k} + \frac{\sum_{k=0}^{L} b_k e^{-j\theta k}}{\hfun} \\ 300 | 1 - \sum_{k=1}^{n} a_k e^{-j\theta k} &= \frac{\sum_{k=0}^{L} b_k e^{-j\theta k}}{\hfun} \\ 301 | \frac{1 - \sum_{k=1}^{n} a_k e^{-j\theta k}}{\sum_{k=0}^{L} b_k e^{-j\theta k}} &= \frac{1}{\hfun}\\ 302 | \hfun &= \frac{\sum_{k=0}^{L} b_k e^{-j\theta k}}{1 - \sum_{k=1}^{n} a_k e^{-j\theta k}} \label{eq:frequency_response} 303 | \end{align} 304 | 305 | \subsection{Z-transform} 306 | \index{Z-transform} 307 | The $z$-transform is a way of analysing the frequency response of a system. It is defined by 308 | \begin{equation} 309 | Z(\xsig) = X(z) = \sum_{n=-\infty}^{\infty} \xsig z^{-n}, 310 | \end{equation} 311 | and has the shifting property 312 | \begin{equation} 313 | Z(x\sqb{n-m}) = z^{-m} \sum_{n=-\infty}^{\infty} x\sqb{n} z^{-n} = z^{-m} X(z). 314 | \end{equation} 315 | 316 | If we apply the $z$-transform to the general difference equation we get 317 | \begin{align} 318 | \ysig = \sum_{k=1}^N a_k y\sqb{n-k} + \sum_{k=0}^L b_k x\sqb{n-k} \\ 319 | Y(z) = \sum_{k=1}^N a_k z^{-k} Y(z) + \sum_{k=0}^L b_k z^{-k} X(z) \\ 320 | \end{align} 321 | from which we arrive at 322 | \index{Transfer function} 323 | \begin{equation} 324 | \label{eq:ztransform} 325 | \frac{Y(z)}{X(z)} = \frac{\sum_{k=0}^L b_k z^{-k}}{1 - \sum_{k=1}^N a_k z^{-k}}, 326 | \end{equation} 327 | which is called the \textit{transfer function}. Note that setting $z = e^{j\theta}$ makes the above equation equal to the frequency response(Eq. \ref{eq:frequency_response}). 328 | 329 | \index{Poles} 330 | \index{Zeros} 331 | We use the above equation to solve for \textit{poles} and \textit{zeros} of the system. The zeros are found by solving the equation 332 | \begin{equation} 333 | \sum_{k=0}^L b_k z^{-k} = 0, 334 | \end{equation} 335 | which is the numerator of Eq. \ref{eq:ztransform} --- when this is zero, the system response will be zero. 336 | 337 | To find the poles, we solve for the numerator like 338 | \begin{equation} 339 | 1 - \sum_{k=1}^N a_k z^{-k} = 0, 340 | \end{equation} 341 | which gives us the places where the ratio explodes into infinity. 342 | 343 | In reality the poles and zeros will cancel each other out by different amounts based on the input frequency. Therefore, the response will seldom actually be zero or infinity. 344 | 345 | \include{sections/reverb} 346 | 347 | \section{Recipes} 348 | \subsection{Echo and Delay} 349 | \index{Echo effect} 350 | \index{Delay effect} 351 | An \textit{echo} effect, in its simplest form, is achieved by mixing a signal with a delayed version of itself 352 | \begin{equation} 353 | \ysig = x\sqb{n} + b_m x\sqb{n - m}, 354 | \end{equation} 355 | where $m$ is the delay amount in samples and $b_m$ the amplitude of the echo. 356 | 357 | The above equation will only give one reflection, if you want multiple reflections, you will need a feedback component: 358 | \begin{equation} 359 | \ysig = x\sqb{n} + a_m y\sqb{n - m}, 360 | \end{equation} 361 | 362 | \index{Dampening} 363 | You can get creative with delay effects by adding more delay taps, or applying effects to the delayed components --- a low-pass filter can be used to achieve a dampening effect. 364 | 365 | \subsection{Chorus} 366 | \index{Chorus effect} 367 | The \text{chorus effect} is achieved by mixing a signal with a delayed version of itself, while varying the delay. The delay usually varies between 20 and 50 ms. This causes a doubling effect, like when multiple instruments are playing together. 368 | 369 | The basic effect looks like 370 | \begin{equation} 371 | \ysig = x\sqb{n} + x\sqb{n - g\sqb{n}} 372 | \end{equation} 373 | where $g\sqb{n}$ is the function determining the delay for sample $n$. 374 | 375 | You can easily get creative with the chorus effect by using feedback instead of delay, and by adding multiple delays. 376 | 377 | \subsection{Flanging} 378 | \index{Flanging} 379 | The \text{flanger effect} is achieved by mixing a signal with a delayed version of itself, while varying the delay. The delay is usually below 20 ms. This creates constructive and destructive interference at specific frequencies, resulting in a swooshing sound. 380 | 381 | The basic effect looks like 382 | \begin{equation} 383 | \ysig = x\sqb{n} + x\sqb{n - g\sqb{n}} 384 | \end{equation} 385 | where $g\sqb{n}$ is the function determining the delay for sample $n$. 386 | 387 | You can easily get creative with the flanger effect by using feedback instead of delay, and by adding multiple delays. 388 | 389 | \subsection{Equal Power Panning and Fading} 390 | \index{Panning} 391 | \index{Fading} 392 | \index{Equal power panning} 393 | \index{Power} 394 | If you wish to do a fade between two signals or position a source in a stereo field, you cannot use a simple linear transition. Since humans perceive sound level in terms of \textit{power}, we will need to pan with equal power, rather than equal amplitude. This is done by using the equation 395 | \begin{equation} 396 | \ysig = \sqrt{r} \cdot x_1\sqb{n} + \sqrt{1 - r} \cdot x_1\sqb{n}, 397 | \end{equation} 398 | to fade between the signals $x_1\sqb{n}$ and $x_2\sqb{n}$ where $r$ is the amount of fade, defined between 0 and 1. 399 | 400 | To pan a signal between two outputs (one stereo channel) we use, 401 | \begin{align} 402 | y_l\sqb{n} &= \sqrt{r} \cdot x\sqb{n} \\ 403 | y_r\sqb{n} &= \sqrt{1 - r} \cdot x\sqb{n}, 404 | \end{align} 405 | where $r = 0.5$ is the center position. 406 | 407 | \subsection{Ring Modulation and Amplitude Modulation} 408 | \index{Ring modulation} 409 | \index{Amplitude modulation} 410 | \index{AM|see {Amplitude modulation}} 411 | Ring modulation is a simple but powerful effect achieved by multiplying two signals, 412 | \begin{equation} 413 | \ysig = x_m\sqb{n} \cdot x_c\sqb{n}, 414 | \end{equation} 415 | where $x_m\sqb{n}$ is the modulator signal, $x_c\sqb{n}$ is the carrier signal. 416 | 417 | Amplitude modulation (AM) is a related effect, achieved by mixing some of the modulated signal back into the output, 418 | \begin{equation} 419 | \ysig = A_m \cdot x_m\sqb{n} \cdot x_c\sqb{n} + A_c \cdot x_c\sqb{n}, 420 | \end{equation} 421 | where $A_m$ is the amplitude of the modulated signal, and $A_c$ is the amplitude of the carrier signal. 422 | 423 | \subsection{Biquad Filters} 424 | \index{Biquad filter} 425 | \index{Second order filter} 426 | Biquad filters are a general form of second-order two pole, two zeros filters. They have the difference equation 427 | \begin{equation} 428 | H(z) = \frac{b_0 + b_1 z^{-1} + b_2 z ^{-2}}{1 + a_1 z_{-1} + a_2 z_{-2}}. 429 | \end{equation} 430 | 431 | If we multiply this by $z^2 / z^2$ we get 432 | \begin{equation} 433 | H(z) = \frac{b_0 z^2 + b_1 z + b_2}{z^2 + a_1 z + a_2 z}, 434 | \end{equation} 435 | and see that both the numerator and denominator are quadratic equations, hence the name. 436 | 437 | Biquad filters can be used to implement many common types of filter including low-pass, high-pass, bandpass, band-reject and all-pass. 438 | 439 | For calculating the coefficients of these filters we will need 440 | \begin{equation} 441 | K = \tan (\pi f_c / f_s), 442 | \end{equation} 443 | where $f_c$ is the filter center frequency and $f_s$ is the sample rate. 444 | 445 | \subsubsection{Low-pass Filter} 446 | \index{Low-pass filter (biquad)} 447 | Low-pass biquad filters are designed by selecting a cutoff frequency $f_c$, and a $Q$ factor which controls the gain around $f_c$. 448 | When $Q = 1/\sqrt 2$ the filter will be flat around $f_c$ --- values above or below will cause amplification or attenuation. 449 | 450 | The coefficients are then calculated by: 451 | \begin{align} 452 | b_0 &= \frac{K^2Q}{K^2Q + K + Q}\\ 453 | b_1 &= \frac{2K^2Q}{K^2Q + K + Q}\\ 454 | b_2 &= b_0\\ 455 | a_1 &= \frac{2Q(K^2 - 1)}{K^2Q + K + Q}\\ 456 | a_2 &= \frac{K^2Q - K + Q}{K^2Q + K + Q} 457 | \end{align} 458 | \cite{Zolzer:2011} 459 | 460 | \subsubsection{High-pass Filter} 461 | \index{High-pass filter (biquad)} 462 | High-pass biquad filters are designed by selecting a cutoff frequency $f_c$, and a $Q$ factor which controls the gain around $f_c$. 463 | When $Q = 1/\sqrt 2$ the filter will be flat around $f_c$ --- values above or below will cause amplification or attenuation. 464 | 465 | The coefficients are then calculated by: 466 | \begin{align} 467 | b_0 &= \frac{K^2Q}{K^2Q + K + Q}\\ 468 | b_1 &= - \frac{2Q}{K^2Q + K + Q}\\ 469 | b_2 &= \frac{Q}{K^2Q + K + Q}\\ 470 | a_1 &= \frac{2Q(K^2 - 1)}{K^2Q + K + Q}\\ 471 | a_2 &= \frac{K^2Q - K + Q}{K^2Q + K + Q} 472 | \end{align} 473 | \cite{Zolzer:2011} 474 | 475 | \section{References} 476 | \bibliography{dsp-cookbook}{} 477 | \bibliographystyle{apalike} 478 | 479 | \printindex 480 | \end{document} 481 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | SECTIONS=$(wildcard sections/*.tex) 2 | 3 | dsp-cookbook.pdf: dsp-cookbook.tex dsp-cookbook.ind dsp-cookbook.blg $(SECTIONS) 4 | pdflatex dsp-cookbook.tex 5 | pdflatex dsp-cookbook.tex 6 | 7 | dsp-cookbook.blg: dsp-cookbook.tex dsp-cookbook.bib 8 | pdflatex dsp-cookbook.tex 9 | bibtex dsp-cookbook 10 | 11 | dsp-cookbook.ind: dsp-cookbook.tex 12 | pdflatex dsp-cookbook.tex 13 | makeindex dsp-cookbook.idx 14 | 15 | .PHONY: clean, open 16 | 17 | open: dsp-cookbook.pdf 18 | open dsp-cookbook.pdf 19 | 20 | clean: 21 | rm -f *.aux 22 | rm -f *.ilg 23 | rm -f *.log 24 | rm -f *.idx 25 | rm -f *.ind 26 | rm -f *.pdf 27 | rm -f *.toc 28 | rm -f *.toc 29 | rm -f *.bbl 30 | rm -f *.blg 31 | rm -f *.out 32 | 33 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # The DSP Cookbook 2 | This is the repository of the document *The DSP Cookbok*, which aims to be a collection of notes on all major digital signal processing topics. 3 | 4 | Please feel free to contribute to the cookbook by writing new sections, proofreading old sections, or by giving general feedback and suggestions. Just send a pull request to this repository. 5 | 6 | If you find a mistake in the cookbook please open a [GitHub issue](https://github.com/PelleJuul/dsp-cookbook/issues) so that someone can fix it. Of course you are also welcome to fix it yourself. 7 | 8 | ## Building the Document 9 | To clone and build the document do the following 10 | 11 | ``` 12 | $ git clone https://github.com/PelleJuul/dsp-cookbook.git 13 | $ cd dsp-cookbook 14 | $ make 15 | ``` 16 | 17 | You can also do it manually by doing 18 | 19 | ``` 20 | $ pdflatex dsp-cookbook.tex 21 | $ makeindex dsp-cookbook idx 22 | $ pdflatex dsp-cookbook.tex 23 | ``` 24 | 25 | Lastly, you can view the document through [ShareLatex](https://www.sharelatex.com/project/59edb2810c9bbf1a840564fc). 26 | 27 | ## Writing Style 28 | 29 | * In titles, the first and last words should be capitalized. Words less than four letters should not be capitalized. 30 | * Equations should be part of the textual flow --- part of the sentence. If the sentence continues after an equation, the new line should not begin with a capital letter. Proper punctuation should be used after equations, such as a period, if the equation end the sentence. 31 | -------------------------------------------------------------------------------- /sections/reverb.tex: -------------------------------------------------------------------------------- 1 | \subsection{Reverb} 2 | \index{Reverb} 3 | In any real sound there will be some amount of \textit{reverb} or \textit{reverberation}, which gives 4 | the sound a spatial character. This is caused by the sound reflecting off the walls and objects of the 5 | environment (room), creating a myriad of indistinguishable. 6 | 7 | \begin{figure}[h] 8 | \centering 9 | \begin{subfigure}{0.3\textwidth} 10 | \centering 11 | \begin{tikzpicture}[scale=1.5, domain=0:4] 12 | \draw (0,0) rectangle (2,4); 13 | \draw [->] (1, 3) -- (1, 1) node [pos=0.5, right, align=left] {\textit{direct}}; 14 | \draw (1, 3) circle [radius=0.1] node [right=5, align=left] {source}; 15 | \draw (1, 1) circle [radius=0.1] node [right=5, align=left] {listener}; 16 | \end{tikzpicture} 17 | \end{subfigure} 18 | \begin{subfigure}{0.3\textwidth} 19 | \centering 20 | \begin{tikzpicture}[scale=1.5, domain=0:4] 21 | \draw (0,0) rectangle (2,4); 22 | \draw [->] (1, 3) -- (0, 2) -- (1, 1) node [pos=0.3, right, align=left] {\textit{early}}; 23 | \draw (1, 3) circle [radius=0.1] node [right=5, align=left] {source}; 24 | \draw (1, 1) circle [radius=0.1] node [right=5, align=left] {listener}; 25 | \end{tikzpicture} 26 | \end{subfigure} 27 | \begin{subfigure}{0.3\textwidth} 28 | \centering 29 | \begin{tikzpicture}[scale=1.5, domain=0:4] 30 | \draw (0,0) rectangle (2,4); 31 | \draw [->] (1, 3) -- (0.3, 4) -- (0, 3.7) -- (2, 0.3) -- (1.8, 0) -- (1, 1) node [pos=0.2, left, align=right] {\textit{late}} ; 32 | \draw (1, 3) circle [radius=0.1] node [right=5, align=left] {source}; 33 | \draw (1, 1) circle [radius=0.1] node [right=5, align=left] {listener}; 34 | \end{tikzpicture} 35 | \end{subfigure} 36 | \end{figure} 37 | 38 | The character of the reverb is highly dependent on the shape of the room. Irregularities and materials of real rooms makes real-time simulation 39 | unsuitable. Therefore reverb is often modelled via the following three types of reflections: 40 | 41 | \begin{description} 42 | \item[direct reflections] that travel directly from the source to the listener. 43 | \item[early reflections] where the sound is reflected off close by surfaces. 44 | \item[late reflections] where the sound is reflected multiple times or off distant surfaces. This is usually very dense with hundreds 45 | or thousands reflections. 46 | \end{description} 47 | 48 | The transmission of sound in air and reflections off the surfaces of the room causes an exponential decay in amplitude. The time it takes for the level to be attenuated $60dB$ is referred to as the \textit{reverberation time}. 49 | \\~\\ 50 | \textit{For more information on reverbs, \cite{Gardner:2002} is recommended.} 51 | --------------------------------------------------------------------------------