How to express or write short integers in MATLAB?
조회 수: 9 (최근 30일)
이전 댓글 표시
Hi there, I am replicating some lines of code from C language in MATLAB evironment. Although I have completed the code but I am still getting some issues therefore, I am having some issue in the generated DATA.
Kindly if someone tell me how to properly use short in MATLAB as shown in the following picture

Here is what I am currently doing in my code
i_acc = bitshift(int16(i_acc+64),-7);
q_acc = bitshift(int16(q_acc+64),-7);
% % converting data
% i_acc = typecast(i_acc, 'int16');
% q_acc = typecast(q_acc, 'int16');
%
% Buffer
iq_buff(isamp*2)= i_acc;
iq_buff(isamp*2+1) = q_acc;
댓글 수: 1
Bjorn Gustavsson
2022년 6월 14일
You'll get more better help faster if you explain what "some issue" is.
답변 (1개)
Arjun
2024년 10월 8일
I see that you want to convert code from C language to MATLAB code.
In MATLAB, you can use int16 in the same way you use short in C code. In the converted code you provided, there are some dissimilarities with the C code. You have type-cast the values and then performed the bit operation, but in the original C code, the order of operations is different, which can result in inconsistent values.
Here is how you can correct it:
% Assuming i_acc and q_acc are previously defined integers
i_acc = bitshift(i_acc + 64, -7); % Equivalent to (i_acc + 64) >> 7
q_acc = bitshift(q_acc + 64, -7); % Equivalent to (q_acc + 64) >> 7
% Assuming iq_buff is a preallocated array and isamp is an index
iq_buff(isamp*2) = int16(i_acc); % Store as a 16-bit integer
iq_buff(isamp*2 + 1) = int16(q_acc); % Store as a 16-bit integer
Kindly go through the documentation of “int16” for better understanding: https://www.mathworks.com/help/releases/R2022a/matlab/ref/int16.html
I hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!