Standard library: Numerics
Decibel Factor
Goal: implement functions to convert from Decibel values to factors and vice-versa.
Steps:
Implement the
Decibels
package.
Implement the
To_Decibel
function.Implement the
To_Factor
function.
Requirements:
The subtypes
Decibel
andFactor
are based on a floating-point type.Function
To_Decibel
converts a multiplication factor (or ratio) to decibels.
For the implementation, use \(20 * log_{10}(F)\), where F is the factor/ratio.
Function
To_Decibel
converts a value in decibels to a multiplication factor (or ratio).
For the implementation, use \(10^{D/20}\), where D is the value in Decibel.
Remarks:
The Decibel is used to express the ratio of two values on a logarithmic scale.
For example, an increase of 6 dB corresponds roughly to a multiplication by two (or an increase by 100 % of the original value).
You can find the functions that you'll need for the calculation in the
Ada.Numerics.Elementary_Functions
package.
Root-Mean-Square
Goal: implement a function to calculate the root-mean-square of a sequence of values.
Steps:
Implement the
Signals
package.
Implement the
Rms
function.
Requirements:
Subtype
Sig_Value
is based on a floating-point type.Type
Signal
is an unconstrained array ofSig_Value
elements.Function
Rms
calculates the RMS of a sequence of values stored in an array of typeSignal
.
See the remarks below for a description of the RMS calculation.
Remarks:
The root-mean-square (RMS) value is an important information associated with sequences of values.
It's used, for example, as a measurement for signal processing.
It is calculated by:
Creating a sequence \(S\) with the square of each value of an input sequence \(S_{in}\).
Calculating the mean value \(M\) of the sequence \(S\).
Calculating the square-root \(R\) of \(M\).
You can optimize the algorithm above by combining steps #1 and #2 into a single step.
Rotation
Goal: use complex numbers to calculate the positions of an object in a circle after rotation.
Steps:
Implement the
Rotation
package.
Implement the
Rotation
function.
Requirements:
Type
Complex_Points
is an unconstrained array of complex values.Function
Rotation
returns a list of positions (represented by theComplex_Points
type) when dividing a circle inN
equal slices.
See the remarks below for a more detailed explanation.
You must use functions from
Ada.Numerics.Complex_Types
to implementRotation
.Subtype
Angle
is based on a floating-point type.Type
Angles
is an unconstrained array of angles.Function
To_Angles
returns a list of angles based on an input list of positions.
Remarks:
Complex numbers are particularly useful in computer graphics to simplify the calculation of rotations.
For example, let's assume you've drawn an object on your screen on position (1.0, 0.0).
Now, you want to move this object in a circular path — i.e. make it rotate around position (0.0, 0.0) on your screen.
You could use sine and cosine functions to calculate each position of the path.
However, you could also calculate the positions using complex numbers.
In this exercise, you'll use complex numbers to calculate the positions of an object that starts on zero degrees — on position (1.0, 0.0) — and rotates around (0.0, 0.0) for N slices of a circle.
For example, if we divide the circle in four slices, the object's path will consist of following points / positions:
We can also describe this path in terms of angles. The following list presents the angles for the path on a four-sliced circle:
Point #1: 0.00 degrees Point #2: 90.00 degrees Point #3: 180.00 degrees Point #4: -90.00 degrees (= 270 degrees) Point #5: 0.00 degrees
To rotate a complex number simply multiply it by a unit vector whose arg is the radian angle to be rotated: \(Z = e^\frac{2 \pi}{N}\)