Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An easy version of the method for using chaos to encrypt an audio message described in Strogatz's Nonlinear dynamics and chaos, 1994
%% Encrypt with chaos
% An easy version of the method for using chaos to encrypt an audio message
% described in Strogatz's Nonlinear dynamics and chaos, 1994
%
% More information at: <http://mappingignorance.org/2016/05/09/the-sound-of-chaos/>
%% Load the original sound
load handel;
sampling_freq = Fs;
signal = y';
length = size(signal, 2);
%% Harvest chaos from the Lorenz equation
% The Lorenz equation is the classical example of deterministic dynamical
% system giving rise to chaos. The equation reads:
%
% $$\dot x = d \left( y - x \right)$$
%
% $$\dot y = r x - y - e x z$$
%
% $$\dot z = -bz + e x y $$
%
% With the following set of parameters
%
% $$d = 10, r = 28, b = \frac{8}{3}, e = 1$$
%
% we will find chaos for almost all initial conditions, being the only
% exception the coordinate origin (an unstable steady state).
%
% So now we can pose the problem in Matlab code
d = 10;
r = 28;
b = 8/3;
e = 1;
fun = @(t,x) [d*(x(2) - x(1));
r*x(1) - x(2) - e.*x(1).*x(3);
-b*x(3) + e.*x(1).*x(2)];
%% Solve numerically
% The Lorenz equation is not analytically solvable, so we will solve it
% numerically for a given initial condition
x0 = [0; 1; 0]; % Set initial condition
tSpan = [0 300]; % Set time span
sol = ode45(fun, tSpan, x0); % Create solver object
% Resample with the desired time step
t = linspace(tSpan(1), tSpan(end), length); % We want a vector with the same...
x = deval(sol, t); % ... length as the signal
%% Extract a one-dimensional time series
% An easy way to extract a one-dimensional time series out of the Lorenz
% attractor is focusing only on one coordinate. Z in our case
timeSeriesX = x(1,:);
timeSeriesZNormalized = timeSeriesX./max(abs(timeSeriesX));
%% Use it as encoding key
strength = 5000;
key = strength*timeSeriesZNormalized;
coded_signal = signal + key;
decoded_signal = coded_signal - key;
%% Plot the signals
figure;
subplot(4,1,1);
plot(t, signal, 'Color', 'b');
title('Signal');
subplot(4,1,2);
plot(t, key, 'Color', 'r');
title('Key');
subplot(4,1,3);
plot(t, coded_signal, 'Color', 'g');
title('Coded signal (Signal + Key)');
subplot(4,1,4);
plot(t, decoded_signal, 'Color', 'b');
title('Recovered signal (Coded - Key)');
%% Export to audio files
%
% The _sonification_ process follows this steps:
%
% * Normalize the signal to the range [-1,1]
% * Pick a sampling frequency
% * Export as _.wav_
%
% Normalization
codedNormalized = coded_signal./max(abs(coded_signal));
decodedNormalized = decoded_signal./max(abs(decoded_signal));
% Exporting
audiowrite('signal.wav', signal, sampling_freq);
audiowrite('coded.wav', codedNormalized, sampling_freq);
audiowrite('decoded.wav', decodedNormalized, sampling_freq);
%% For direct playback use
%
% soundsc(decodedNormalized, sampling_freq);
%
%% Credits
% Pablo Rodríguez-Sánchez
%
% <https://sites.google.com/site/pablorodriguezsanchez/>
%
% April 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.