This commit is contained in:
Silas Oettinghaus
2023-05-15 15:55:16 +02:00
parent 55db794162
commit 0086346efd
6 changed files with 265 additions and 38 deletions

193
Classes/Scope.m Normal file
View File

@@ -0,0 +1,193 @@
classdef Scope
%SCOPE Summary of this class goes here
% Detailed explanation goes here
properties
fsimu
fadc
adcresolution
quantbuffer
rand_samplingdelay %on or off
samplingdelay %specifiy a sampling delay
freq_offset %offset of the sampler
samp_jitter %include jitter
fixed_delay %fix the delay of the filter or use minimal delay for kausal system
delay %specify a fixed delay of the filter
filtertype
lpf_bw
%during construction
%during process
Nout
end
methods
function obj = Scope(options)
%SCOPE Construct an instance of this class
% Detailed explanation goes here
arguments
options.fsimu
options.fadc
options.adcresolution
options.quantbuffer
options.rand_samplingdelay = 0;
options.samplingdelay = 0;
options.freq_offset = 0;
options.samp_jitter = 0;
options.fixed_delay = 0;
options.delay = 0;
options.filtertype = filtertypes.bessel_bilin;
options.lpf_bw = 120e9;
end
obj.fsimu = options.fsimu;
obj.fadc = options.fadc;
obj.adcresolution = options.adcresolution;
obj.quantbuffer = options.quantbuffer;
obj.rand_samplingdelay = options.rand_samplingdelay; % use a randomized sample delay INSTEAD of samplingdelay
obj.samplingdelay = options.samplingdelay; %specifiy a sampling delay
obj.freq_offset = options.freq_offset; %offset of the sampler
obj.samp_jitter = options.samp_jitter; %include jitter in [s]
obj.fixed_delay = options.fixed_delay; %fix the delay of the filter or use minimal delay for kausal system
obj.delay = options.delay; %specify a fixed delay of the filter
obj.filtertype = options.filtertype;
obj.lpf_bw = options.lpf_bw;
end
function yout = process(obj,xin)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
% apply LPF
lpf = Filter('filtdegree',4,"f_cutoff",obj.lpf_bw,"fsamp",obj.fsimu,"filterType",obj.filtertype);
yout = lpf.process(xin);
% sample signal
% TODO: implement and test the delays. Also look for delay of
% lpf filter
yout = obj.sampleSignal(yout);
% quantize signal
yout = obj.quantize(yout);
end
function jitvec = buildSamplingJitterVector(obj)
% Generation of sampling jitter vector for real part of signal
jitvec = obj.samp_jitter*randn(obj.Nout,1) ;
% Avoid exceding the simulation block edges relentlessly!
jitvec(1) = abs(jitvec(1)) ;
jitvec(end) = -abs(jitvec(end)) ;
end
function sampout = sampleSignal(obj,xin)
% Initialization sampler and output signal in case of 4real
Tout = 1 / obj.fadc; % output signal sample period [sec]
Tsig = length(xin) / obj.fsimu ; % simulation block period [sec]
if obj.rand_samplingdelay == 1
obj.samplingdelay = Tout*rand ;
end
%get number of delayed samples
Ndelay = floor(obj.samplingdelay*obj.fsimu) ;
%get fractional sample delay
fracdelay = obj.samplingdelay - Ndelay/obj.fsimu ;
%calc number of samples after down-sampling no offset
obj.Nout = ceil((Tsig - fracdelay)*obj.fadc) ;
%build a jitter vector
jitvec = obj.buildSamplingJitterVector();
% Create the index vector of the sampling instances for the real and imaginary part
inx_re = ( (0: Tout : (obj.Nout-1) / obj.fadc)' + jitvec ) * obj.fsimu + 1 ;
% Create the vector of the relative location of each sample between
% to neighboring "Analog" samples for the real and imaginary part
val_re = mod(inx_re,1) ;
% Shift the signal
xin = circshift(xin, [0 - Ndelay]) ;
% Sample the signal using linear interpolation
sampout = xin(floor(inx_re)) .* (1 - val_re) + xin(ceil(inx_re)) .* val_re ;
end
function quantout = quantize(obj,xin)
quant_steps = round(2^obj.adcresolution) + rem(round(2^obj.adcresolution),2);
if quant_steps > 0
index = round(0.1*length(xin)); %sioe 2022: short blocksize resulted in error -> now 10%-90% of length are evaluated
max_re = max(real(xin(index:end-index)))*(1+obj.quantbuffer/2); % adaptations in the indices
min_re = min(real(xin(index:end-index)))*(1-obj.quantbuffer/2); %(1+para.max/2);
range_re = max_re-min_re;
max_im = max(imag(xin))*(1+obj.quantbuffer/2);
min_im = min(imag(xin))*(1+obj.quantbuffer/2);
range_im = max_im-min_im;
if isreal(xin)
% shift signal and clip to quantizer intervall
xin = min(max(xin-min_re,0),range_re);
% quantize signal
xin = round((quant_steps-1)/range_re*xin);
% scale and shift back
quantout = xin*range_re/(quant_steps-1) +min_re;
else
% shift signal and clip to quantizer intervall
xin_re = min(max(real(xin)-min_re,0),range_re);
xin_im = min(max(imag(xin)-min_im,0),range_im);
% quantize signal
% quantize signal and shift back signal
xin_re = round((quant_steps-1)/range_re*xin_re);
xin_im = round((quant_steps-1)/range_im*xin_im);
% scale and shift back
quant_out_re = xin_re*range_re/(quant_steps-1) + min_re;
quant_out_im = xin_im*range_im/(quant_steps-1) + min_im;
quantout = quant_out_re + 1i * quant_out_im;
end
else
quantout = xin;
end
end
end
end