%fan - f_analog: sampling of the "analog" signal
fan = 10; %freqency in KHz (SI) of analog "samples"
% Time for signal -- 1000 samples, 100 milliseconds
t = 0:1/fan:100;
t = t(1:end-1);

% The product of these two numbers must be 100
% numberOfPulses = 20; % Number of discrete values to be sent
% durationMs = 5; % Duration of each discrete value (ms)
numberOfPulses = 5; % Number of discrete values to be sent
durationMs = 20; % Duration of each discrete value (ms)

% % Original Signal. Random 3-bit data.
xin = floor(8*rand(1,numberOfPulses));
% Convert to 0.1 volt increments
xin = xin/10;
% Extend each signal to be constant for a short time
xin = kron(xin,ones(1,durationMs*fan));

% xin = [rand*ones(1,166), rand*ones(1,167), rand*ones(1,167)];
% xin = [xin xin];


% Noise and delay
%% Very nice channel
tau = 1; % delay, in milliseconds;
peakNoise = 0.05;
%% Delayed channel
% tau = 12; % delay, in milliseconds;
% peakNoise = 0.05;
%% Noisy channel
% tau = 1; % delay, in milliseconds;
% peakNoise = 0.25;
%% Delayed, noisy channel
% tau = 12; % delay, in milliseconds;
% peakNoise = 0.25;
%% Perfect channel
% tau = 0;
% peakNoise = 0;

%% Simulate and plot
range = round([length(xin)-tau*fan+1:length(xin), 1:length(xin)-tau*fan]);
xout = xin(range) + peakNoise*2*(rand(size(xin))-0.5);
plot(t,xin,t,xout);

% % filter noise
% filtSize = 100;
% boxFilt = 1/filtSize*ones(1,filtSize);
% xfilt = filter(boxFilt,1,[xout, xout]);
% xfilt = xfilt(end/2+1:end);
% plot(t,xin,t,xfilt);

ylabel('Voltage (V)')
xlabel('Time (ms)')
shg
