forked from yueyuzhao/gyrophone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_audio.m
36 lines (30 loc) · 910 Bytes
/
generate_audio.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
%% Generate audio for sound feedback analysis on a phone
function generate_audio(output_file)
FS = 8000;
FREQS = [90];
AUDIO_LEN = 10; % in seconds
NUM_SAMPLES = AUDIO_LEN * FS;
AMP = .5; % amplitude
% generate audio
samples = zeros(1, NUM_SAMPLES);
for f = FREQS
x = zeros(1, NUM_SAMPLES);
for i=1:NUM_SAMPLES
x(i) = AMP * sin(2 * pi * f * (i-1) / FS);
end;
samples = samples + x;
end
samples = normalize(samples, AMP);
% show FFT
delta = FS/NUM_SAMPLES;
plot(-FS/2:delta:FS/2-delta, fftshift(abs(fft(samples))));
% play audio
player = audioplayer(samples, FS);
play(player);
pause(AUDIO_LEN);
% save audio to file
audiowrite(output_file, samples, FS);
end
function normalized = normalize(samples, max_amp)
normalized = samples / (max(samples)/max_amp);
end