In programming, a loop is a control structure used for repeating commands several times. Rather than having to type of the repeating commands each time, a programmer uses the loop to make writing the code more efficient and easier to read.
Computers excel at repeating commands very quickly. Therefore, the loop construct is very common in programming. We will use loops in several different ways when working with audio.
In Matlab, there are a couple different ways to create loops. The first way we will consider is called a “for loop.” As part of creating the loop, we need to specify in our script exactly which lines of code should be repeated, as well as how many times they should repeat.
Therefore, it is necessary to use keywords at the start and end of the loop. Additionally, a counting variable is created to keep track of how many times a loop repeats. While executing a loop, this counting variable can be included in commands.
A loop is an important concept for processing audio signals. When our signal is stored in an array, we would like to index through the individual elements in order to change the value of the signal’s samples.