Skip to content

Commit

Permalink
reduce octave ddpm execution by profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
bat52 committed Mar 31, 2023
1 parent 225133c commit 6cb5d55
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 22 deletions.
33 changes: 26 additions & 7 deletions src/octave/ddpm_mod.m
Expand Up @@ -6,14 +6,33 @@

ddpm_s = zeros(size(s));

for tidx = 1:length(s)
for bidx = 1:nbits
if bitand(count(tidx), (2^(nbits-bidx)-1) ) == 2^(nbits-1-bidx)
if bitand(s(tidx),bitshift(1,bidx))
ddpm_s(tidx) = 1;
end
if 0
% loop form (slower)

for tidx = 1:length(s)
for bidx = 1:nbits
if bitand(count(tidx), (2^(nbits-bidx)-1) ) == 2^(nbits-1-bidx)
if bitand(s(tidx),bitshift(1,bidx))
ddpm_s(tidx) = 1;
end
end
end
end
end

else
% matrix form (faster)

for bidx = 1:nbits
idxs = find(
(bitand(count, (2^(nbits-bidx)-1) ) == 2^(nbits-1-bidx) ) &
( bitand(s,bitshift(1,bidx)) > 0 )
);

% size(idxs)

ddpm_s(idxs) = ones(size(idxs));
end

end

end
34 changes: 33 additions & 1 deletion src/octave/sd1_mod.m
Expand Up @@ -15,10 +15,18 @@
minval = 0;

for idx = 2:length(inval)
delta(idx) = saturated_adder( inval(idx), qerr(idx-1), maxval, minval);
if 1
delta(idx) = saturated_adder( inval(idx), qerr(idx-1), maxval, minval);
else
% just slightly faster
delta(idx) = inval(idx) + qerr(idx-1);
end
qerr(idx) = bitand( delta(idx), 2^(nbits-nbits_out) - 1);
sd_out(idx) = bitshift(delta(idx),-(nbits-nbits_out));
end

% check for saturation
saturation_check(delta, maxval, minval);

if 1
fh = figure(541);
Expand Down Expand Up @@ -53,6 +61,20 @@

ret = a + b;

if ret > maxval
ret = maxval;
else
if ret < minval
ret = minval;
end
end

end

function ret = saturated_adder_vec(a, b, maxval, minval)

ret = a + b;

maxidx = find(ret > maxval);
ret(maxidx) = maxval * ones(size(maxidx));

Expand All @@ -61,3 +83,13 @@

end

function saturation_check(s, maxval, minval)

maxidx = find(s > maxval);
assert(isempty(maxidx));

minidx = find(s < minval);
assert(isempty(maxidx));

end

43 changes: 29 additions & 14 deletions src/octave/sim_top.m
Expand Up @@ -3,7 +3,9 @@
clear all;
close all;

pkg load signal
profile off;
profile clear;
profile on;

% inputs
fclock = 12.5; # khz
Expand All @@ -16,8 +18,16 @@
nbit_pwm_phase = 8;
nbit_sine_phase = 8;
nbit_pwm_amplitude = 8;
nperiods = 5;
nperiods = 5;
end

if 0
nbit_pwm_phase = 10;
nbit_sine_phase = 6;
nbit_pwm_amplitude = nbit_pwm_phase;
nperiods = 3;
end

nbit_sd_amplitude = nbit_pwm_amplitude + 2;

% parameters
Expand Down Expand Up @@ -66,16 +76,16 @@
xlim((1/fclock)*[0 (2^nbit_phase)-1]);
end

if 0
figure(111);
stairs(t,s);
hold on; grid on;
stairs(t,max(s)*sd1_s,'r');
xlabel('time [ms]');
ylabel('amplitude [LSB]');
legend('quant sine','sd1');
xlim((1/fclock)*[0 (2^nbit_phase)-1]);
end
##if 0
## figure(111);
## stairs(t,s);
## hold on; grid on;
## stairs(t,max(s)*sd1_s,'r');
## xlabel('time [ms]');
## ylabel('amplitude [LSB]');
## legend('quant sine','sd1');
## xlim((1/fclock)*[0 (2^nbit_phase)-1]);
##end

if 1
fh = figure(1111);
Expand Down Expand Up @@ -108,7 +118,7 @@
xlim((1/fclock)*[0 (2^nbit_phase)-1]);

saveas(fh, 'timedomain', 'png');
close(fh)
% close(fh)
end

%% freq domain plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -147,4 +157,9 @@
legend('pwm', 'ddpm', 'sd1','Location','southwest');
saveas(fh, 'freqdomain', 'png');
% close(fh)
end
end

profile off;
prof = profile ('info');
profshow(prof);
% profexplore(prof);

0 comments on commit 6cb5d55

Please sign in to comment.