% Synthesize at a rate chosen by:
%   voltage steps_per_cycle
% Analyse at a rate chosen by dt
% Assumes: fixed sample time, continusously varying amp
clear all

dt = 0.001; % analysis rate
% compute exactly one cycle of sine
t = [0:dt:2*pi]; % Times at which to sample the sine function
sig = sin(1*t); % Original signal, a sine wave

% === vary this to test n-sanple approximation =========
steps_per_cycle = 32;
% ======================================================

%quantize to few time steps
% and build the waveform
value = 0; 
step = fix(length(t)/steps_per_cycle); 
running_i = 0;

for i=1:length(t)
    s(i) = value ;
    if running_i==step
        value = sig(i);
        running_i = 0 ;
    end
    running_i = running_i + 1 ;    
end

% time series plot
figure(1);clf
subplot(2,1,1)
plot(t,s, 'linewidth',2)
title(['steps/cycle=' num2str(steps_per_cycle)])

% fourier transform plot
% main peak is always at fft index 2, which is frequency=1 cycle
% (index 1 is DC in matlab)
subplot(2,1,2)
plot(log10(abs(fft(s))), 'linewidth',2)
set(gca, 'xlim',[0 120])
ylabel('log(mag)')    


