278
Appendix B: Software
B.10 Entropy of Audio Files
The probability distribution of sound amplitudes of the first movement of Beethoven’s
fifth symphony that we show in Fig. 12.4 is based on the following script, which first
defines a function g to describe a Gaussian before loading the audio file with MATLAB’s built-in audioread() function, which returns the samples in the variable
y and the sampling frequency fs. Next, we assign left and right channel to the variables l and r and calculate the rms and the mean of the sum of the channels. The
histogram of amplitudes is returned by the call to the hist() function, which prepares a histogram of the input samples, from which we calculate the probabilities tt p
of the amplitudes. Before calculating the entropy from (12.20), we remove samples
with zero probability, which do not contribute to the entropy. For comparison, we
prepare a Gaussian with the same rms as Beethoven’s symphony and calculate its
entropy, before plotting both.
% audio_analysis.m
clear all; close all;
g=@(x,m,sig)exp(-((x-m).ˆ2)./(2*sig.ˆ2))./sqrt(2*pi*sig.ˆ2);
[y,fs]=audioread(’beethoven_S5_M1.wav’);
l=y(:,1); r=y(:,2); sigma=rms(l+r); avg=mean(l+r);
[N,x]=hist(l+r,1000);
dx=x(2)-x(1);
p=N/(sum(N)*dx); pp=p(p>0);
en_beethoven=-sum(pp.*log(pp))*dx
% entropy, (12.20)
yg=g(x,avg,sigma); en_gauss=-sum(yg.*log(yg))*dx; % gauss
en_analytical=0.5*log(2*pi*exp(1)*sigmaˆ2) % (12.24)
plot(x,p,’k’,x,g(x,avg,sigma),’r--’,’LineWidth’,2)
xlabel(’x=u/u_0’); ylabel(’dN/dx’);
legend(’Beethoven’,’Gauss’)
B.11 Elliptic Curves
The addition of points that lie on an elliptic curve is discussed in the beginning of
Sect. 12.7 and defined in (12.32) and (12.33). The function ECadd_p() implements these equations. It receives the coordinates of points two points P a and P b , the
coefficients a and b that define the elliptic curve in (12.30), and the prime p. The
function returns the coordinate of the point P c = P a ⊕ P b . Inside the function, we
first catch the exceptional conditions related to the additional point at infinity that
serves as “zero” for the addition of points. Then we code (12.33) with the slope s
defined by (12.32), where differentiate the case with P a = P b and P a = P b . Note
that we use (12.34) to calculate the denominator in (12.32). In order to prevent numer-
Appendix B: Software
B.10 Entropy of Audio Files
The probability distribution of sound amplitudes of the first movement of Beethoven’s
fifth symphony that we show in Fig. 12.4 is based on the following script, which first
defines a function g to describe a Gaussian before loading the audio file with MATLAB’s built-in audioread() function, which returns the samples in the variable
y and the sampling frequency fs. Next, we assign left and right channel to the variables l and r and calculate the rms and the mean of the sum of the channels. The
histogram of amplitudes is returned by the call to the hist() function, which prepares a histogram of the input samples, from which we calculate the probabilities tt p
of the amplitudes. Before calculating the entropy from (12.20), we remove samples
with zero probability, which do not contribute to the entropy. For comparison, we
prepare a Gaussian with the same rms as Beethoven’s symphony and calculate its
entropy, before plotting both.
% audio_analysis.m
clear all; close all;
g=@(x,m,sig)exp(-((x-m).ˆ2)./(2*sig.ˆ2))./sqrt(2*pi*sig.ˆ2);
[y,fs]=audioread(’beethoven_S5_M1.wav’);
l=y(:,1); r=y(:,2); sigma=rms(l+r); avg=mean(l+r);
[N,x]=hist(l+r,1000);
dx=x(2)-x(1);
p=N/(sum(N)*dx); pp=p(p>0);
en_beethoven=-sum(pp.*log(pp))*dx
% entropy, (12.20)
yg=g(x,avg,sigma); en_gauss=-sum(yg.*log(yg))*dx; % gauss
en_analytical=0.5*log(2*pi*exp(1)*sigmaˆ2) % (12.24)
plot(x,p,’k’,x,g(x,avg,sigma),’r--’,’LineWidth’,2)
xlabel(’x=u/u_0’); ylabel(’dN/dx’);
legend(’Beethoven’,’Gauss’)
B.11 Elliptic Curves
The addition of points that lie on an elliptic curve is discussed in the beginning of
Sect. 12.7 and defined in (12.32) and (12.33). The function ECadd_p() implements these equations. It receives the coordinates of points two points P a and P b , the
coefficients a and b that define the elliptic curve in (12.30), and the prime p. The
function returns the coordinate of the point P c = P a ⊕ P b . Inside the function, we
first catch the exceptional conditions related to the additional point at infinity that
serves as “zero” for the addition of points. Then we code (12.33) with the slope s
defined by (12.32), where differentiate the case with P a = P b and P a = P b . Note
that we use (12.34) to calculate the denominator in (12.32). In order to prevent numer-
