clear all;

K  = 30;                 % number of averaged simulations
N  = 2^10;               % simulation length (output samples)
fs = 1;                  % sampling frequency 
T  = 1/fs;               % time step
fx = pi/50;              % signal frequency

choice = 'dc';

if strcmp(choice, 'sin')
    A  = 0.79;               % signal amplitude
    offset = pi/150;
    ti = sprintf('First order sigma-delta, sin input, A=%g, fx=%g, offset=%g, N=%d, K=%d', A, fx, offset, N, K);
elseif strcmp(choice, 'dc')
    A  = 0;                  % signal amplitude
    offset = 1/11;
    ti = sprintf('First order sigma-delta, DC input, offset=%g, N=%d, K=%d', offset, N, K);
end

Y = zeros(N, K);         % result vector

for i=1:K
    [t,x,y] = sim('sigma_delta_L1_sim', T*(N-1));
    Y(:, i) = y(:, 3);
end

[f, p, pi] = avg_spectrum(Y);

plot(f, 10*log10(p));
xlabel('Frequency   [ f/f_s ]');
ylabel('Amplitude   [ dBWN ]');
axis([0 0.5 -50 30]);
title(ti);
