- /
-
Simple Bounce
on 16 Oct 2024
- 13
- 138
- 0
- 3
- 1031
See code for audio in the function make_bounce_audio below.
drawframe(1)
function drawframe(f)
t = 4*(0:95)/96;
tnow = t(f);
y = 1-((tnow-2).^2)/4;
x = interp1([0 1 2 3 4],[0 1 0 -1 0],tnow);
plot(x,y,'.',MarkerSize=48)
axis(1.1*[-1 1 0 1])
set(gca,XTick=[],YTick=[])
end
The make_bounce_audio function is here just for reference. It doesn't run during the creation of the animation.
function make_bounce_audio
% Parameters
Fs = 44100; % Sampling frequency (Hz)
duration = 4; % Total duration of the audio (seconds)
beep_freq_hi = 880; % Frequency of the beep (Hz)
beep_freq_lo = 440; % Frequency of the beep (Hz)
beep_duration = 0.5; % Duration of each beep (seconds)
alpha = 9.2103; % Exponential decay constant
% Time vector for the entire duration
Ns = Fs * duration;
t = (0:Ns-1)/Fs;
% Time vector for a single beep
t_beep = (0:1/Fs:beep_duration - 1/Fs);
% Exponential envelope
envelope = exp(-alpha * t_beep);
% Beep signal with exponential decay
beep_signal_hi = sin(2 * pi * beep_freq_hi * t_beep) .* envelope;
beep_signal_lo = sin(2 * pi * beep_freq_lo * t_beep) .* envelope;
% Initialize the complete signal with zeros
signal = zeros(1, Ns);
% Indices for the first beep (from 1 sec to 1.5 sec)
start_idx1 = round(0 * Fs) + 1;
end_idx1 = start_idx1 + length(beep_signal_hi) - 1;
% Indices for the second beep (from 2 sec to 2.5 sec)
start_idx2 = round(1 * Fs) + 1;
end_idx2 = start_idx2 + length(beep_signal_lo) - 1;
% Indices for the second beep (from 2 sec to 2.5 sec)
start_idx3 = round(3 * Fs) + 1;
end_idx3 = start_idx3 + length(beep_signal_hi) - 1;
% Insert the beeps into the signal
signal(start_idx1:end_idx1) = beep_signal_lo;
signal(start_idx2:end_idx2) = beep_signal_hi;
signal(start_idx3:end_idx3) = beep_signal_hi;
% Normalize the signal if necessary
if max(abs(signal)) > 1
signal = signal / max(abs(signal));
end
sound(signal,Fs)
plot(t,signal)
% Write the signal to a .wav file
audiowrite('output.wav', signal, Fs);
end


