Help rewriting a function using If statements and For Loop

2 views (last 30 days)
Hello everyone,
So I have the following code, which is a simple Quantization, that check the value of each element in an array, and round it to an already set value, here I have only 4 levels (1.5 0.5 -0.5 -1.5). And I am assuming that the max amplitude of the input signal is 2.
dq_tx = [];
for i1 = 1:numel(k)
if (d_k(i1) >=1)
dqe = 1.5;
elseif (d_k(i1) >=0)
dqe = 0.5;
elseif (d_k(i1) >=-1)
dqe = -0.5;
else (d_k(i1) <-1);
dqe = -1.5;
dq_tx = [dq_tx dqe];
What I want to do is making it more global, to have the number of levels as an input from the user, and make it depended on the max ampltiude of the input signal as well. And here is what I have so far in terms of the conceptual implicaiton, but It's not working yet and I feel i am missing something
amp=1.2; %max amplitude of the signal as input from the user
L = 4; % # levels in the quantizer
dMin = -amp;
dMax = amp;
%q = 2*dMax/L; % step size
%q2 = q/2;
dR = linspace(dMin,dMax,L+1) % decision intervals
for i=1:L
dR2(i) = ( dR(i) + dR(i+1)) ./ 2; %Re-adjusting the intervals values
d_k=[1 0.5 -0.2 -0.7 -1] %Assumed message signal values
%Qunatization Code
dq_tx = [];
for i1 = 1:numel(k)
if (d_k(i1) >=dR2(end))
dqe = dR2(end);
for i2=1:L-2
if (d_k(i1) >=dR2(end-i2))
dqe = dR2(end-i2);
else (d_k(i1) <dR2(1)); % Correct the greater than sign
dqe = dR2(1);
dq_tx = [dq_tx dqe];
Stephen23 on 7 Dec 2022
"I tried to use it but the issue ... it actually shouldn't be equally split..."
Neither DISCRETIZE nor HISTCOUNTS require the edges to be "equally split". They are just simpler ways of doing what you want, without needing to reinvent the wheel.

Sign in to comment.

Accepted Answer

James Water Bird
James Water Bird on 6 Dec 2022
to make your code more global and adaptable to different input signal amplitudes and number of quantization levels, you can make the following changes to your code:
  • Use a for loop to iterate over the values in the input signal d_k and apply the quantization rules to each value.
  • Use the input signal amplitude amp and number of levels L to calculate the step size q and decision intervals dR.
  • Use an if statement to compare the current input value d_k(i1) to the decision intervals dR and determine which quantized value dqe to use.
% Input parameters
amp = 1.2; % max amplitude of the input signal
L = 4; % number of quantization levels
% Calculate step size and decision intervals
q = 2*amp/L; % step size
dR = linspace(-amp, amp, L+1); % decision intervals
% Input signal
d_k = [1 0.5 -0.2 -0.7 -1];
% Quantization
dq_tx = []; % initialize quantized output signal
for i1 = 1:numel(d_k)
dqe = 0; % initialize quantized value
% Compare input value to decision intervals
if d_k(i1) >= dR(end) % greater than or equal to maximum interval
dqe = dR(end); % use maximum interval value
for i2 = 1:L-1
if d_k(i1) >= dR(end-i2) && d_k(
Abdelrhman Abdelfatah
Abdelrhman Abdelfatah on 7 Dec 2022
Thank you so much, I figured it out and it's working like magic now!

Sign in to comment.

More Answers (0)


Find more on Geographic Plots in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!