# Physics 203 at Portland State 2014

### Site Tools

digital_sound_project
A PCRE internal error occured. This might be caused by a faulty plugin

====== Differences ====== This shows you the differences between two versions of the page.

digital_sound_project [2014/05/19 06:05]
wikimanager [Software]
digital_sound_project [2014/06/04 03:53] (current)
Line 1: Line 1:
======Digitizing the sound====== ======Digitizing the sound======
-==By Alisha Harrington, Christopher Hernandez, ​and Aaron Heston==+==By Alisha Harrington, Christopher Hernandez, Aaron Heston, and Samia Maurice==
==in collaboration with Prof. Nicholas Kuzma== ==in collaboration with Prof. Nicholas Kuzma==

Line 82: Line 82:
* The possible values are usually of the form $V_i=\Delta V\!\cdot\!v_i$       * The possible values are usually of the form $V_i=\Delta V\!\cdot\!v_i$
* where $\Delta V$ is the overall gain scale (measured and recorded in volts per bit (V/bit))         * where $\Delta V$ is the overall gain scale (measured and recorded in volts per bit (V/bit))
-        * and $v_i\in \big\{-\!2^{\,​n-1}\!+\!1,$ $- 2^{\,​n-1}\!+\!2,$ $...$ $-1,$ $0,$ $1,$ $2,$ $...$ $2^{\,n-1} \big\}$ are integers+        * and $v_i\in \big\{-\!2^{\,​n-1},​$ $- 2^{\,​n-1}\!+\!1,$ $...$ $-1,$ $0,$ $1,$ $2,$ $...$ $2^{\,n-1}\!-\!1 ​\big\}$ are integers
* $\in$ symbol means "with possible values from the following set"           * $\in$ symbol means "with possible values from the following set"
* The number $n$ is called the **bit depth**, **number of bits**, or **bit resolution** of the recorder, usually $n=8,$ $12,$ or $16$.           * The number $n$ is called the **bit depth**, **number of bits**, or **bit resolution** of the recorder, usually $n=8,$ $12,$ or $16$.
-            * For example, in an 8-bit recorder, the integers $v_i$ can range from $-127$ to $128$ (in total, $2^8\!=\!256$ possible levels)  +            * For example, in an 8-bit recorder, the integers $v_i$ can range from $-128$ to $127$ (in total, $2^8\!=\!256$ possible levels)
-        * the most negative recorded voltage is $V_\text{min}=\left(-2^{\,n-1}\!+1\right)\Delta V$ +        * the most negative recorded voltage is $V_\text{min}=-2^{\,​n-1}\Delta V$
-          * For an 8-bit recorder, this would evaluate to $V_\text{min}=-127\!\cdot\!\Delta V$ +          * For an 8-bit recorder, this would evaluate to $V_\text{min}=-128\!\cdot\!\Delta V$
-        * the most positive recorded voltage is $V_\text{max}=2^{\,​n-1}\Delta V$ +        * the most positive recorded voltage is $V_\text{max}=\left(2^{\,n-1}\!-1\right)\Delta V$
-          * For an 8-bit recorder, this would evaluate to  $V_\text{max}=128\!\cdot\!\Delta V$+          * For an 8-bit recorder, this would evaluate to  $V_\text{max}=127\!\cdot\!\Delta V$
* The smallest voltage difference that can be recorded is $\Delta V$. It is sometimes called **voltage quantization scale**       * The smallest voltage difference that can be recorded is $\Delta V$. It is sometimes called **voltage quantization scale**
* Substituting the continuous voltage $V(t_i)$ with the nearest possible $\Delta V\!\cdot\!v_i$ can cause some [[wp>​Quantization (signal processing)|quantization errors]] for weak sounds       * Substituting the continuous voltage $V(t_i)$ with the nearest possible $\Delta V\!\cdot\!v_i$ can cause some [[wp>​Quantization (signal processing)|quantization errors]] for weak sounds
Line 155: Line 155:
* In the command window, type: <​code>​playsound wave1</​code>​       * In the command window, type: <​code>​playsound wave1</​code>​
* If no sound is audible, please make sure your computer volume is not muted or set to zero (i.e. you can hear a [[http://​youtube.com|YouTube]] video)       * If no sound is audible, please make sure your computer volume is not muted or set to zero (i.e. you can hear a [[http://​youtube.com|YouTube]] video)
+        * If there is still no sound (on Windows), try to increase the amplitude of your sound wave: <​code>​wave1=2000*sin(2*pi*1500*x)
+playsound wave1</​code>​
- Displaying the wave as a time plot     - Displaying the wave as a time plot
* In the command window, type <​code>​display wave1</​code> ​       * In the command window, type <​code>​display wave1</​code> ​
* Or, build a new graph via a "​Windows"​ menu, "New Graph..."​ submenu. ​       * Or, build a new graph via a "​Windows"​ menu, "New Graph..."​ submenu. ​
-        * Choose ​"​calculate" ​for the x-wave (this will utilize the "​scaling"​)+        * Choose ​''​_calculated_'' ​for the x-wave (this will utilize the "​scaling"​)
* To zoom in on the plot, drag a box (called **marquee**) across the area you want to zoom in/out with the left button using a mouse.       * To zoom in on the plot, drag a box (called **marquee**) across the area you want to zoom in/out with the left button using a mouse.
* Release the mouse button, then click inside the marquee to select the zoom option         * Release the mouse button, then click inside the marquee to select the zoom option
Line 165: Line 167:
- Recording the sound... follow these steps:     - Recording the sound... follow these steps:
- Type in the command window, hitting the "​Enter"​ key after each line:<​code>​SoundInStatus       - Type in the command window, hitting the "​Enter"​ key after each line:<​code>​SoundInStatus
-edit W_SoundInRates</​code>​ +edit W_SoundInRates
-        ​The first command gets IGOR to ask your computer'​s sound system for possible sampling rates +print V_SoundInSampSize</​code>​
-        * The answers ​are stored in an automatically created ''​W_SoundInRates''​ wave +        ​The first command gets IGOR to ask your computer'​s sound system for possible sampling rates and other parameters ​
-        ​The second command simply displays this wave as an editable table +          * The possible sampling rates are stored in an automatically created ''​W_SoundInRates''​ wave
-        * The first value is the number of possible values (usually 5 or 7), can be visible in the top left corner of Fig. 2 +        ​The second command simply displays this wave as an editable table
-        * After that all the possibilities are listed in units of ${\text s}^{-1}$. +          * The first value is the number of possible values (usually 5 or 7), can be visible in the top left corner of Fig. 2
-      - Create an empty wave (see above) ​and set the sampling rate to one of the possibilities (using the ''​SetScale''​ command) +          * After that all the possibilities are listed in units of ${\text s}^{-1}$.
-      - In the command window, type <​code>​SoundInRecord ​wave1</​code>​ +        - The third command prints the automatically created variable V_SoundInSampSize in the command window'​s history area
-        * <color red>​Warning</​color>:​ This command will erase any data previously stored in ''​wave1''​ +          * On most windows laptops this value is 3:
-          * Use ''​Duplicate ​wave1 anotherwave''​ to store all the ''​wave1''​ data (including the scaling) in ''​anotherwave''​ +            * it means, the sound card can only record into an array (wave) of 8-bit or 16-bit integers.
-        * If an error comes up, check you sampling rate againIt should match one of the choices exactly.+          * On a typical Mac, this value is 10:
+            * it means the sound card can also record into an array (wave) of real (floating-point) numbers
+      - Create an empty wave and set the sampling rate to one of the possibilities (using the ''​SetScale''​ command): <​code>​Make/​O/​W/​N=131072 wave2
+FindLevel/​Q/​R=[1,​numpnts(W_SoundInRates)-1] W_SoundInRates,​ 40000
+Variable/G bestrate=W_SoundInRates[min(W_SoundInRates[0],​SelectNumber(V_Flag,​round(V_LevelX),​2))]
+Setscale/P x 0, 1/​bestrate,"​s",​wave2</​code>​
+        - Here, the first line created a brand-new wave ''​wave2''​ filled with zeroes and having a default scaling (no units, start 0, step 1)
+          * The ''/​O''​ option enables overwriting any previous data stored in ''​wave2''​ without producing an error
+          * The ''/​W''​ option makes this a 16-bit integer wave (with possible values limited to integers from $-32768$ to $32767$)
+            * On a Mac, if the value of ''​V_SoundInSampSize''​ is greater than 3, you can get a better-quality recording by not using ''/​W''​
+          * The number of points is $N=131072=2^{17}$:​ chosing a power of 2 turns Fourier transform into a Fast Fourier Transform
+        - The second line searches the possible sampling rates in ''​W_SoundInRates'':​
+          * ''/​Q''​ is the "​quiet"​ mode, prevents printing the result in history and will not give an error if the value is not found
+          * ''/​R=[1,​numpnts(W_SoundInRates)-1]'':​
+            * the search begins at point 1 (the 2<​sup>​nd</​sup>​ value in the table showing ''​W_SoundInRates'',​ since IGOR counts indices from 0)
+            * ends at the last point (= # of points $-1$, because the points are counted starting from 0)
+          * looking for a sampling rate closest to 40000 samples per second, so that
+            * the upper limit of the human hearing range is close to the Nyquist limit of $\frac{1}{2}f_\text{samp}=20\,​$kHz
+          * if the search doesn'​t find anything, the variable ''​V_Flag''​ is set to 1
+          * if the result is found, ''​V_flag''​ is set to 0, and ''​V_LevelX''​ is set to the number close to the index (x-scaling) of the result
+        - The 3<​sup>​rd</​sup>​ command sets a new ''​bestrate''​ variable to one of the possible sampling rates depending on the following:​
+          * If ''​V_flag''​ is 1 (no sampling rate close to 40000 found), the third number in ''​W_SoundInRates''​ is picked (index=2)
+          * If ''​V_flag''​ is 0 (sampling rate close to 40000 was found), the index obtained in the search is rounded to the nearest integer
+          * the ''​min(,​)''​ ensures that the index does not exceed the # of choises stored in the zeroth element of ''​W_SoundInRates''​
+          * the ''​W_SoundInRates[...]''​ selects the sampling rate from the ''​W_SoundInRates''​ wave stored in the calculated index'​th element
+        - The fourth command sets the wave scaling to the inverse of the sampling rate stored in ''​bestrate''​ variable
+      - In the command window, type <​code>​SoundInRecord ​wave2</​code>​
+        * <color red>​Warning</​color>:​ This command will erase any data previously stored in ''​wave2''​
+          * Use ''​Duplicate ​wave2 anotherwave''​ to store all the ''​wave2''​ data (including the scaling) in ''​anotherwave''​, to preserve it
+        * If an error comes up, check the sampling rate of ''​wave2'' ​again: <​code>​print 1/​deltax(wave2)</​code> ​It should match one of the choices ​in ''​W_SoundInRates'' ​exactly
+          * If an error comes up on a Mac, try to rerun all the commands in step 9.b without the ''/​W''​ in the ''​Make''​ command:<​code>​Make/​O/​N=131072 wave2
+FindLevel/​Q/​R=[1,​numpnts(W_SoundInRates)-1] W_SoundInRates,​ 40000
+Variable/G bestrate=W_SoundInRates[min(W_SoundInRates[0],​SelectNumber(V_Flag,​round(V_LevelX),​2))]
+Setscale/P x 0, 1/​bestrate,"​s",​wave2</​code>​
* The length of the recording is determined by the number of points in your wave (the record length $N$) and the sampling rate $f_\text{samp}$         * The length of the recording is determined by the number of points in your wave (the record length $N$) and the sampling rate $f_\text{samp}$
- to make sure the recording is successful, play it back (see above).       - to make sure the recording is successful, play it back (see above).
- Calculating and displaying the power spectrum of the sound as a function of frequency     - Calculating and displaying the power spectrum of the sound as a function of frequency
-      * In the command window, type <​code>​FFT/​MAGS /DEST=wave1_fft wave1; display ​wave1_fft; ModifyGraph log(left)=1</​code>​+      * In the command window, type <​code>​FFT/​MAGS /DEST=wave2_fft wave2; display ​wave2_fft; ModifyGraph log(left)=2</​code>​
* This is an example of executing 3 different IGOR commands on the same line:       * This is an example of executing 3 different IGOR commands on the same line:
* The commands in this case need to be separated by a semicolon '';''​         * The commands in this case need to be separated by a semicolon '';''​
- The first command ​         - The first command ​
-          * computes the squared magnitude (power) of the [[wp>​discrete-time Fourier transform]] of ''​wave1''​ as a function of frequency +          * computes the squared magnitude (power) of the [[wp>​discrete-time Fourier transform]] of ''​wave2''​ as a function of frequency
-          * saves it as a ''​wave1_fft''​ wave+          * saves it as a ''​wave2_fft''​ wave
* note that the scaling of the ''​wave1_fft''​ wave is automatically in units of frequency (Hz) with the correct spacing $\Delta f$           * note that the scaling of the ''​wave1_fft''​ wave is automatically in units of frequency (Hz) with the correct spacing $\Delta f$
-          * <color red>​Warning</​color>:​ This command will erase any data previously stored in ''​wave1_fft''​ +          * <color red>​Warning</​color>:​ This command will erase any data previously stored in ''​wave2_fft''​
-            * Use ''​Duplicate ​wave1_fft ​anotherfft''​ to store all the ''​wave1_fft''​ data (including the scaling) in ''​anotherfft''​ +            * Use ''​Duplicate ​wave2_fft ​anotherfft''​ to store all the ''​wave2_fft''​ data (including the scaling) in ''​anotherfft''​
-        - The second command creates a new default-style graph of the power spectrum contained in ''​wave1_fft''​+        - The second command creates a new default-style graph of the power spectrum contained in ''​wave2_fft''​
- The third command changes the type of the vertical axis of the graph to log-type, so the weaker harmonics and overtones can be seen         - The third command changes the type of the vertical axis of the graph to log-type, so the weaker harmonics and overtones can be seen
Line 196: Line 231:
* Open the saved file to get back to where you finished last time         * Open the saved file to get back to where you finished last time
* Access your file either from the Mac finder or Windows explorer, or from IGOR's "​File"​ menu, "​Recent Experiments"​ submenu           * Access your file either from the Mac finder or Windows explorer, or from IGOR's "​File"​ menu, "​Recent Experiments"​ submenu
+    - Automation
+      * If you find yourself running the same commands over and over (trying to record something and plot the spectrum, for example)
+        * It might be the right time to automate your tasks:
+        - First, copy the commands you tend to repeat from the history area of the Command window
+        - Go to the Procedure window (''​Ctrl-M''​ or ''​Cmd-M''​) and create an empty macro shell there, by typing below the ''#​pragma''​ line:<​code>​Macro MyMacro()
+
+End</​code> ​
+          * Here, ''​MyMacro''​ is the name of your macro, it can be any word without spaces or punctuation characters
+          * ''​()''​ indicates that you do not have any input variables or strings
+        - Paste the often-used commands between the ''​MyMacro()''​ and ''​End''​ lines, make sure each command is on a separate line.
+        - Compile the code by clicking "​compile"​ at the bottom of the Procedure window or by switching to a Command window
+        - Go back to the Command window (''​Ctrl-J''​ or ''​Cmd-J''​) and run your macro by typing <​code>​MyMacro()</​code>​ Or, you can run your macro from the "​Macros"​ menu

Line 210: Line 257:
=====Data===== =====Data=====
+  - {{ :​projects:​wave_2_graph.png?​200|}} I have been following the directions for the digital sound project for a Windows computer. This graph of a wave was generated (see figure on the right); however, I haven'​t recorded anything. In fact, I haven'​t figured out how to record anything at all like the examples on this wiki page. I am hoping someone can tell me how to proceed.
+    * **Answer**: What makes you think you haven'​t recorded anything? I think you did!  I can see some noises and a loud bang in the beginning. It looks like a recording. If you post your saved IGOR experiment (assuming the file is not more than a few MB - it shouldn'​t be with just one recording), we can listen to the sound. ​
+      * Probably you haven'​t made any sound while recording: ​ why not repeat the recording step again, but this time,
+      * as soon as you hit "​Enter"​ on the ''​SoundInRecord wave2''​ command, start singing a note, like "​Eeeeeeeeeeee",​ for about 3 seconds. ​
+        * No need to plot again, - the existing plot of wave2 will update automatically.
+      * Also, try playing back your recording by entering ''​playsound wave2''​ into command window. Do you hear your "​Eeeeeeee"?​
+  - Okay,I did record a sound. I would like to post it so we can hear it and you can give me suggestions for experimenting with it or with additional sounds. However, when I tried to upload the file I received this message " Upload denied. This file extension is forbidden!"​
+    * **Answer**: Good job! Sorry I didn't realize you cannot upload igor files to the wiki.  One workaround is to change the extension from pxp to txt, that might work - I am not sure. You can also email me the pxp file as an attachment.
+      * I think you are on the right track. You should learn how to zoom in and out on the plot in Igor (by dragging a box with the left button of a mouse and then clicking inside the box) - do it repeatedly until you see the individual oscillations (See figure below). ​ You can zoom all the way out by pressing ''​Ctrl-A''​. Look at the fine details of your plots. ​ ''​Wave2''​ sounds like a piano note - do you have an electric piano or a piano app on your phone? ​ I can also hear a dog barking and people talking outside - all this information is contained in the column of numbers that you recorded - I wonder which digits contain which sound. {{ :​projects:​sound:​clipping.png?​nolink |}}
+      * Anyways, if you zoom in on the beginning part of wave2, as shown here on the second plot of the same ''​wave2'',​ you will see that the amplitude is unnaturally constant for a while (before it starts dropping off), and the oscillations look more like square waves rather than sine waves. This tells me that your sound was too loud for the range of your recorder - that is, the actual sound was decaying in amplitude all along from the start, but the recorder was pegged at the maximum of what it can record (that is, "​clipping your sound"​) until the sound decayed enough to "​fit"​ into the range of the recorder. Does it make sense? ​  Try to repeat the same recording, but either make the sound quieter, or move the laptop away from the source so that there is some attenuation (or even do what humans do - cover your microphone (if you know which hole it is on the laptop) with a cloth or a towel.)
+      * Another tool (besides zooming in on the time course of the signal) is to look at the spectrum - this wiki page explains how to do this. For your beautiful piano-like tone, this should give a very nice thin line in the spectrum, if you are not clipping with your recorder. If you are clipping, like in ''​wave2''​ now, the spectrum will have extra stuff - the musicians can tell it's clipping because it sounds more "​metallic"​ or even "​buzzing"​. ​ You can dedicate the entire project to the effect of clipping - compare clipped and not clipped sound (the perceived impression, the time plots, and the spectra).  ​
+  - I have collected some initial data and recordings. In my file:
+    * ''​Wave2''​ is a medium volume sound directly adjacent to the microphone.
+      * Wave 2 obviously translates poorly and exhibits clipping.
+    * ''​Wave3''​ is a soft volume sound directly adjacent to the microphone.
+      * Wave 3 sounds ok despite its close proximity to the microphone.
+    * ''​Wave4''​ is a sound of varying volume at typical lapotop distance (my final data will have a constant pitch, this was just for my own
+    * But I have graphed all three spectra according to your directions, and they look nearly identical. What do I need to do to discern meaningful data from these graphs? ​
+    * **Answer**: To compare the spectra directly, plot them on the same graph by <​code>​display wave2_fft,​wave3_fft,​wave4_fft;​
+ModifyGraph log(left)=1,​rgb(wave3_fft)=(0,​0,​65535),​rgb(wave4_fft)=(0,​0,​0);​
+SetAxis bottom 0,​4000</​code>​
+      * Actually, somehow all your fourier transforms are the same!   ​That'​s why they look the same! They ended up being identical.
+        * Do them again: <​code>​FFT/​MAGS /​DEST=wave2_fft wave2;
+FFT/MAGS /​DEST=wave3_fft wave3;
+FFT/MAGS /​DEST=wave4_fft wave4;</​code> ​
+        *Look at the most recent plot you just plotted. ​ Now you see differences.
+      * Also makes sense to plot the time-traces on the same plot: <​code>​display wave2,​wave3,​wave4;​
+ModifyGraph rgb(wave2)=(65535,​40000,​40000),​rgb(wave3)=(0,​0,​65535),​rgb(wave4)=(0,​0,​0);​
+SetAxis bottom 0,​.04</​code> ​
+        * This highlights the striking differences in intensity.
+  -  The other thing is I have been looking at Fourier series calculations and have seen the use of integrals when looking at the Fourier coefficients. Should I be concerned about this?
+    * **Answer**: there are two kinds of Fourier transforms: [[wp>​Fourier transform|continuous]] (liked by theorists) and [[wp>​Discrete Fourier transform|discrete]] (used in numerical computations). The two kinds are closely related, and in the limit of small grid spacing (small dwell time $\Delta t$) they approximately give the same results. But, the continuous transform uses integrals, whereas the discrete one uses simple sums.  You don't need to worry about this too much, just use it as a tool that already works. When you write about it, just say that it decomposes the time-dependent signal into a linear superposition of pure sine and cosine waves at various frequencies on a frequency grid, and gives you the combined amplitude at each frequency, as a function of the point on the frequency grid.
+  - Is there a way to copy and paste the plots from Igor pro into a document or is there another way to display the plots in a document?
+    * **Answer**: You can use
+      * "​screenshots"​ (will work for any screen content, not just IGOR plots) ​
+        * explained in [[white_noise_project#​software_and_data_analysis|the white noise project, Software and data analysis, Item 10]] <- click on the green link
+      * Or, in IGOR, after clicking on your graph, go to the ''​File''​ menu, ''​Save Graphics...''​ submenu, and select ​
+        * the format (PNG or JPEG_
+        * resolution (Other DPI, then 300)
+        * file name -- the name of the picture file that IGOR will create for you
+        * path (home -- will save in the same directory on your computer where you have saved your .pxp experiment
+        * force overwrite -- if you already have that picture file and want to improve it
+      * Then just insert the picture file into your report as picture

-=====References=====+====Tips and suggestions====
+  - You can simulate clipping by using the following commands (assuming you want to clip ''​wave2''​ that already exists):
+    * the function ''​=min(...,​...)''​ returns the smaller of the two arguments
+    * the function ''​=max(...,​...)''​ returns the larger of the two arguments
+    * before you start, you need to set up your ''​low''​ and ''​high''​ variables fist: <​code>​Wavestats/​Q wave2; print V_min,V_max // this line finds the min and max of wave2
+Variable/G clip_perc=80 // this creates a variable for storing the clipping percentage
+Variable/G low=clip_perc*V_min/​100,​high=clip_perc*V_max/​100 // sets low and high thresholds for clipping</​code>​
+      * Here the ''​%%//​%%''​ is the "​comment"​ symbol, it tells IGOR to ignore the remainder of the line, allowing you to comment your code
+    * now create another wave and combine the ''​min()''​ and ''​max()''​ as ''​max(low,​min(high,​...))'':​ <​code>​Duplicate/​O wave2,​wave2_clipped // create an identical wave2_clipped first
+wave2_clipped=max(low,​min(high,​wave2)) // now do the clipping</​code>​ to limit the values in wave2_clipped to be within the [low,high] range
+    * you can achieve different levels of clipping by setting ''​clip_perc''​ closer to 0 (more severe clipping) or closer to 100 (more gentle)
+    * you can display the clipped wave as a plot, calculate and display its spectrum, and play it to listen how it sounds
+
+=====References ​and Footnotes=====
+====Cited references====
- Strutt ([[wp>​John William Strutt, 3rd Baron Rayleigh|Rayleigh]]),​ J W; Lindsay, R B (1877). The Theory of Sound. Dover Publications. ISBN 0-4866-0292-3.   - Strutt ([[wp>​John William Strutt, 3rd Baron Rayleigh|Rayleigh]]),​ J W; Lindsay, R B (1877). The Theory of Sound. Dover Publications. ISBN 0-4866-0292-3.
- From N.K.'s past contributions to the [[wp>​Sound]] article on [[wp>​Wikipedia]].   - From N.K.'s past contributions to the [[wp>​Sound]] article on [[wp>​Wikipedia]].
+
+====Footnotes====