Help rewriting a function using If statements and For Loop

3 Ansichten (letzte 30 Tage)
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;
end
dq_tx = [dq_tx dqe];
end
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
dR2=zeros(1,L);
i=0;
for i=1:L
dR2(i) = ( dR(i) + dR(i+1)) ./ 2; %Re-adjusting the intervals values
end
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);
elseif
for i2=1:L-2
if (d_k(i1) >=dR2(end-i2))
dqe = dR2(end-i2);
end
end
else (d_k(i1) <dR2(1)); % Correct the greater than sign
dqe = dR2(1);
end
dq_tx = [dq_tx dqe];
end
  5 Kommentare
Abdelrhman Abdelfatah
Abdelrhman Abdelfatah am 7 Dez. 2022
Thank you so much, I figured it out and it's working like magic now!
Stephen23
Stephen23 am 7 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Water Bird
James Water Bird am 6 Dez. 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.
Forexample
% 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
else
for i2 = 1:L-1
if d_k(i1) >= dR(end-i2) && d_k(
  2 Kommentare
Abdelrhman Abdelfatah
Abdelrhman Abdelfatah am 6 Dez. 2022
Okay, that's actually a better implementation, how can I go on with the rest of the code?
I went as, however iti keeps giving me 0 output values
for i2 = 1:L-1
if (d_k(i1) >= dR(end-i2)) && (d_k(i1) <= dR(end-i2-1))
dqe = dR(end-i2);
end
dq_tx = [dq_tx dqe];
end
Abdelrhman Abdelfatah
Abdelrhman Abdelfatah am 7 Dez. 2022
Thank you so much, I figured it out and it's working like magic now!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by