Help rewriting a function using If statements and For Loop
古いコメントを表示
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 件のコメント
Jan
2022 年 12 月 6 日
What about: [BINS, EDGES] = discretize(X,N) ?
Stephen23
2022 年 12 月 6 日
Abdelrhman Abdelfatah
2022 年 12 月 6 日
Abdelrhman Abdelfatah
2022 年 12 月 7 日
Stephen23
2022 年 12 月 7 日
"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.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Audio Processing Algorithm Design についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!