# 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/25 18:09]
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 160: Line 160:
* 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 187: Line 187:
* The ''/​W''​ option makes this a 16-bit integer wave (with possible values limited to integers from $-32768$ to $32767$)           * 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''​             * 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 second line searches the possible sampling rates in ''​W_SoundInRates'':​
* The number of points is $N=131072=2^{17}$:​ chosing a power of 2 turns Fourier transform into a Fast Fourier Transform           * 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            * ''/​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]'': ​           * ''/​R=[1,​numpnts(W_SoundInRates)-1]'': ​
Line 206: Line 206:
* <color red>​Warning</​color>:​ This command will erase any data previously stored in ''​wave2''​         * <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           * Use ''​Duplicate wave2 anotherwave''​ to store all the ''​wave2''​ data (including the scaling) in ''​anotherwave'',​ to preserve it
-        * If an error comes up, check you sampling rate again: <​code>​print 1/​deltax(wave2)</​code>​ It should match one of the choices in ''​W_SoundInRates''​ exactly+        * 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           * 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 FindLevel/​Q/​R=[1,​numpnts(W_SoundInRates)-1] W_SoundInRates,​ 40000
Line 231: 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 245: 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
+
+====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===== =====References and Footnotes=====