Lab 5 - Algorithm Selection

Sound is represented as signed 16-bit integer signal samples. Typically there is one stream of samples for left and right side of the stereo. Each channel sample rate is between 44.1 ~ 48 thousand samples per second, totaling to 88.2 ~ 96 thousand samples per second.
The change of volume requires scaling each sample by the volume factor, which is between 0.0 (silence) to 1.0 (full volume).

Sampling the sound and scaling them to a specific volume requires lot of processing power. This lab will create an array with size of 100 million to represent sound samples. The samples will be scaled by volume factor of 0.75 and store them into another array.

To find the most efficient method, three algorithms will be compared:

  1. Multiply each sample by floating point number 0.75
  2. Pre-calculate a lookup table of all possible sample values multiplied by the volume factor, and look up each sample in that table to get the scaled values
  3. Convert the volume factor 0.75 to a fix-point integer by multiplying a binary number representing a fixed-point value "1". For this I'll be using 256 to represent "1" and shift by 8 (>> 8)
Each algorithms will be compiled with -O3 to get the most optimization.

Method 1:
This algorithm was provided as a benchmark
Code:


Method 2:
Here lookup table is created with the samples pre-calculated with volume factor.
Code:


Method 3:
Here each sample is scaled with fix-point integer.
Code:


I have ran above algorithms in both aarchie and ccharlie for comparison.

Results


Conclusion
As seen from the comparison table, Method 2 takes the least amount of time to execute the code. The difference of amount of memory used aren't significantly different between the methods.
If balance of execution time and memory usage was the deciding factor, Method 1 would be the choice here. Its execution time is about the same as Method 3 but uses the least amount of memory.
If the execution time is the deciding factor, Method 2 would be the choice. It has the quickest execution time, but does use more memory than Method 1 and Method 2.

Comments