%% listing 5.5
sr = 44100; % the sample rate in Hz
f0 = 250; % the frequency of the tone in Hz
d = 1; % the tone?s duration in sec
t = linspace(0, d, sr*d); % a time vector
first_component = sin(2*pi*f0*t); % full amplitude
second_component = 1/2 * sin(2*pi*(f0*2)*t); % 1/2 amplitude
third_component = 1/4* sin(2*pi*(f0*3)*t); % 1/4 amplitude
complex = first_component + second_component + third_component;
plot(complex(1:441))

%% listing 5.6
f0=250; amplitude = 1;
for i = 1:20
    component(i, :) = amplitude * sin(2*pi*(f0*i)*t);
    amplitude = amplitude/2;
end
complex = sum(component);
complex = complex/max(abs(complex));
figure
plot(complex(1:441))

%% listing 5.7
figure;
for i= 1:4
    subplot(2, 2, i);
    if i ~= 4
        plot(component(i, 1:441));
    else
        plot(complex(1:441));
    end
    axis([1, 441, -1, 1])
end