Hack_Audio_7542_C000 2018/6/5 14:15 Page i #1 Hack Audio An Introduction to Computer Programming and Ⓡ Digital Signal Processing in MATLAB Computersareatthecenterofalmosteverythingrelatedtoaudio.Whetherforsynthesisin musicproduction,recordinginthestudio,ormixinginlivesound,thecomputerplaysan essentialpart.Audioeffectsplug-insandvirtualinstrumentsareimplementedassoftware computercode.Musicappsarecomputerprogramsrunonamobiledevice.Allthesetoolsare createdbyprogrammingacomputer. HackAudio:AnIntroductiontoComputerProgrammingandDigitalSignalProcessingin MATLABprovidesanintroductionformusiciansandaudioengineersinterestedincomputer programming.Itisintendedforarangeofreadersincludingthosewithyearsofprogramming experienceandthosereadytowritetheirfirstlineofcode.Inthebook,computer programmingisusedtocreateaudioeffectsusingdigitalsignalprocessing.Bytheendofthe book,readersimplementthefollowingeffects:signalgainchange,digitalsumming,tremolo, auto-pan,mid/sideprocessing,stereowidening,distortion,echo,filtering,equalization, multi-bandprocessing,vibrato,chorus,flanger,phaser,pitchshifter,auto-wah,convolution andalgorithmicreverb,vocoder,transientdesigner,compressor,expander,andde-esser. Throughoutthebook,severaltypesoftestsignalsaresynthesized,includingsinewave,square wave,sawtoothwave,trianglewave,impulsetrain,whitenoise,andpinknoise.Common visualizationsforsignalsandaudioeffectsarecreatedincludingwaveform,characteristic curve,goniometer,impulseresponse,stepresponse,frequencyspectrum,andspectrogram.In total,over200examplesareprovidedwithcompletedcodedemonstrations. EricTarrisanassistantprofessorofAudioEngineeringTechnologyatBelmontUniversityin Nashville,TN.Heteachesclassesondigitalaudio,computerprogramming,andsignal processing.HereceivedaPh.D.,M.S.,andB.S.inElectricalandComputerEngineeringfrom theOhioStateUniversity.HereceivedaB.A.inMathematicsandaminorinMusicfrom CapitalUniversityinColumbus,OH.Hisworkhasspannedacrossthetopicsofspeechsignal processing,musicalrobotics,soundspatialization,acousticandelectronicsystemmodeling, hearingloss,perception,andcognition.HehaspublishedarticlesintheJournalofthe AcousticalSocietyofAmerica,JournalofSpeech,Language,andHearingResearch, InternationalJournalofAudiology,andMechanicalEngineeringResearch.Heisamember oftheAudioEngineeringSociety.In2015,Dr.TarrreceivedtheGibsonFoundationLesPaul MusicInnovationAwardastheprincipalinvestigatorofaresearchgranton“Blockchain TechnologyintheMusicIndustry.” Hack_Audio_7542_C000 2018/6/5 14:15 Page ii #2 AUDIOENGINEERINGSOCIETYPRESENTS… http://www.aes.org/ EditorialBoard Chair:FrancisRumsey,LogophonLtd. HyunKookLee,UniversityofHuddersfield NatanyaFord,UniversityofWestEngland KyleSnyder,OhioUniversity OthertitlesintheSeries: HandbookforSoundEngineers,5thEdition EditedbyGlenBallou AudioProductionandCriticalListening,2ndEdition AuthoredbyJasonCorey RecordingOrchestraandOtherClassicalMusicEnsembles AuthoredbyRichardKing RecordingStudioDesign,4thEdition AuthoredbyPhilipNewell ModernRecordingTechniques,9thEdition AuthoredbyDavidMilesHuber ImmersiveSound:TheArtandScienceofBinauralandMulti-ChannelAudio EditedbyAgnieszkaRoginskaandPaulGeluso HackAudio:AnIntroductiontoComputerProgrammingandDigitalSignal ProcessinginMATLABⓇ AuthoredbyEricTarr Hack_Audio_7542_C000 2018/6/5 14:15 Page iii #3 Hack Audio An Introduction to Computer Programming and Ⓡ Digital Signal Processing in MATLAB Eric Tarr Hack_Audio_7542_C000 2018/6/5 14:15 Page iv #4 Firstpublished2019 byRoutledge 711ThirdAvenue,NewYork,NY10017 andbyRoutledge 2ParkSquare,MiltonPark,Abingdon,Oxon,OX144RN RoutledgeisanimprintoftheTaylor&FrancisGroup,aninformabusiness ©2019Taylor&Francis TherightofEricTarrtobeidentifiedasauthorofthisworkhasbeenassertedbyhiminaccordancewithsections77and78of theCopyright,DesignsandPatentsAct1988. Allrightsreserved.Nopartofthisbookmaybereprintedorreproducedorutilisedinanyformorbyanyelectronic,mechanical, orothermeans,nowknownorhereafterinvented,includingphotocopyingandrecording,orinanyinformationstorageor retrievalsystem,withoutpermissioninwritingfromthepublishers. ReprintwithpermissionofTheMathWorks,Inc. MATLAB®andSimulink®areregisteredtrademarksofTheMathWorks,Inc.andareusedwithpermission.TheMathWorks doesnotwarranttheaccuracyofthetextorexercisesinthisbook.Thisbook’suseordiscussionofMATLAB®andSimulink® softwareorrelatedproductsdoesnotconstituteendorsementorsponsorshipbytheMathWorksofaparticularpedagogical approachorparticularuseoftheMATLAB®andSimulink®software. ForMATLABandSimulinkproductinformation,pleasecontact: TheMathWorks,Inc. 3AppleHillDrive Natick,MA01760-2098USA Tel:508-647-7000 Fax:508-647-7001 E-mail:[email protected] Web:mathworks.com Howtobuy:www.mathworks.com/store Trademarknotice:Productorcorporatenamesmaybetrademarksorregisteredtrademarks,andareusedonlyforidentification andexplanationwithoutintenttoinfringe. LibraryofCongressCataloging-in-PublicationData Names:Tarr,Eric,author. Title:Hackaudio:anintroductiontocomputerprogramminganddigitalsignalprocessing inMATLAB/EricTarr. Description:NewYork,NY:Routledge,2019.|Series:AudioEngineeringSocietypresents Identifiers:LCCN2018006035|ISBN9781138497542(hardback:alk.paper)| ISBN9781138497559(pbk.:alk.paper)|ISBN781351018463(ebook) Subjects:LCSH:MATLAB.|Computersoundprocessing. Classification:LCCML74.4.M36T372018|DDC006.5–dc23 LCrecordavailableathttps://lccn.loc.gov/2018006035 ISBN:978-1-138-49754-2(hbk) ISBN:978-1-138-49755-9(pbk) ISBN:978-1-351-01846-3(ebk) TypesetinTimes bydiacriTech Hack_Audio_7542_C000 2018/6/5 14:15 Page v #5 Dedicated to my supportive wife,Kate Hack_Audio_7542_C000 2018/6/5 14:15 Page vi #6 Hack_Audio_7542_C000 2018/6/5 14:15 Page vii #7 Contents List of Tables..............................................................................................xvii List of Figures.............................................................................................xix List of Examples ......................................................................................xxvii Acknowledgements................................................................................xxxiii Chapter 1: Introduction................................................................................1 1.1 Introduction:ComputerProgrammingandDigitalSignalProcessing .............1 1.2 ThePurposeofThisBook............................................................1 1.3 IntendedReaders......................................................................1 1.4 TopicsCovered........................................................................2 1.5 AdditionalContent....................................................................2 Chapter 2: Basics of Programming in MATLABⓇ ........................................3 2.1 Introduction:ComputerProgramminginMATLAB................................3 2.2 ProgrammingLanguages.............................................................3 2.3 ExecutedCommands .................................................................4 2.3.1 ErrorStatements.............................................................4 2.4 Mathematics...........................................................................4 2.4.1 Operators.....................................................................4 2.4.2 Variables .....................................................................6 2.5 DataTypes ............................................................................8 2.5.1 Numbers......................................................................8 2.5.2 Characters....................................................................9 2.5.3 Strings........................................................................9 2.6 Arrays ................................................................................10 2.6.1 BasicArrayCreation .......................................................10 2.6.2 PlottingArrays..............................................................11 2.7 MathematicalFunctions.............................................................12 2.7.1 PlottingMathematicalFunctions ..........................................12 vii Hack_Audio_7542_C000 2018/6/5 14:15 Page viii #8 viii Contents 2.8 APPENDIX:AdditionalPlottingOptions..........................................12 2.8.1 LineSpecification ..........................................................12 2.8.2 AxisLabels .................................................................15 2.8.3 FigureTitle..................................................................15 2.8.4 FigureLegend ..............................................................15 2.8.5 AxisScale...................................................................15 2.8.6 AxisDimensions............................................................16 2.8.7 MultipleArrays.............................................................17 Chapter 3: Basics of Audio in MATLABⓇ ...................................................21 3.1 Introduction:DigitalAudioSignals.................................................21 3.2 MATLABAudioFunctions .........................................................22 3.2.1 audioread .................................................................22 3.2.2 sound .......................................................................22 3.2.3 audiowrite................................................................23 3.2.4 audioinfo .................................................................23 3.3 WorkingwithAudioSignalsinArrays .............................................23 3.3.1 IndexingArrays.............................................................23 3.3.2 ArrayReversal..............................................................26 3.3.3 AdditionalMethodstoCreateArrays .....................................26 3.3.4 ArrayTransposition ........................................................28 3.3.5 DeterminingDimensionsofArrays........................................28 3.4 VisualizingtheWaveformofanAudioSignal .....................................29 Bibliography ..............................................................................30 Chapter 4: MATLABⓇ Programming Environment...................................31 4.1 Introduction:MATLABApplication................................................31 4.1.1 CommandWindow.........................................................31 4.1.2 Workspace ..................................................................33 4.1.3 CurrentFolder ..............................................................34 4.2 MATLABm-Files ...................................................................34 4.2.1 Scripts.......................................................................35 4.2.2 CommentingCode..........................................................36 4.3 MATLABDebuggingMode ........................................................36 4.4 MATLABHelpDocumentation.....................................................38 Chapter 5: Logicals and Control Structures in Programming..................41 5.1 Introduction:ControllingtheFlowofExecution...................................41 5.2 LogicalDataType ...................................................................41 5.2.1 LogicalOperations .........................................................41 5.2.2 CombiningLogicalOperators .............................................43 5.2.3 StringCompare.............................................................44 5.3 TypesofControlStructures .........................................................44 5.3.1 ConditionalStatements.....................................................44 5.3.2 Loops........................................................................47 5.3.3 Functions....................................................................52 Hack_Audio_7542_C000 2018/6/5 14:15 Page ix #9 Contents ix Chapter 6: Signal Gain and DC Offset.......................................................57 6.1 Introduction:DigitalSignalProcessing.............................................57 6.1.1 Element-WiseProcessing ..................................................57 6.1.2 Element-WiseReferencing.................................................57 6.1.3 BlockDiagrams.............................................................58 6.2 ScalarOperationswithArrays ......................................................59 6.2.1 ScalarMultiplication:ConvertingTimeUnits ............................60 6.3 ScalarMultiplication:SignalGain..................................................61 6.3.1 SignalGainBlockDiagram................................................61 6.3.2 PolarityInversion...........................................................63 6.3.3 DecibelScale ...............................................................64 6.4 VisualizingtheAmplitudeChange..................................................66 6.4.1 InputversusOutputCharacteristicCurve .................................66 6.5 ScalarAddition:DCOffset..........................................................68 6.5.1 DCOffsetBlockDiagram..................................................68 6.6 CombinedSignalGainandDCOffset..............................................71 6.7 AmplitudeMeasurements ...........................................................72 6.7.1 SignalPeakAmplitude.....................................................73 6.7.2 Peak-to-PeakAmplitude ...................................................73 6.7.3 Root-Mean-SquareAmplitude.............................................74 6.7.4 DynamicRangeCrestFactor ..............................................75 6.8 AmplitudeNormalization ...........................................................76 6.8.1 PeakNormalization.........................................................76 6.8.2 Root-Mean-SquareNormalization.........................................77 Bibliography ..............................................................................78 Chapter 7: Introduction to Signal Synthesis..............................................79 7.1 Introduction:SignalSynthesis ......................................................79 7.2 VisualizingtheFrequencySpectrumofanAudioSignal..........................79 7.3 PeriodicSignals......................................................................80 7.3.1 SineWave...................................................................80 7.3.2 CosineFunction ............................................................86 7.3.3 SquareWave ................................................................87 7.3.4 SawtoothWave .............................................................91 7.3.5 TriangleWave...............................................................93 7.3.6 ImpulseTrain ...............................................................96 7.4 AperiodicSignals....................................................................99 7.4.1 WhiteNoise.................................................................99 Bibliography ............................................................................ 101 Chapter 8: Digital Summing,Signal Fades,and Amplitude Modulation..............................................................................103 8.1 Introduction:CombiningSignals ................................................. 103 8.1.1 ArrayOperationswithArrays............................................ 103 8.1.2 UsingArrayOperationstoCombineSignals............................ 104