Help rewriting a function using If statements and For Loop
Show older comments
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 Comments
Jan
on 6 Dec 2022
What about: [BINS, EDGES] = discretize(X,N) ?
Stephen23
on 6 Dec 2022
Abdelrhman Abdelfatah
on 6 Dec 2022
Abdelrhman Abdelfatah
on 7 Dec 2022
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.
Accepted Answer
More Answers (0)
Categories
Find more on Audio Processing Algorithm Design 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!