How to improve bit reading process in Matlab?
2 次查看(过去 30 天)
显示 更早的评论
Star Rats
2019-9-6
I need to read a binary file and the Matlab coding is:
a = uint8(fread(fid,'ubit1')');
This is inefficient so I added a line below:
a = uint16(swapbytes(a));
But it doesn't help. Any other method to improve the bit reading process in Matlab?
14 个评论
Walter Roberson
2019-9-6
Is there any reason that you are not reading uint8? And possibly extracting bits afterwards? Either with bitget() or with a lookup table ?
Star Rats
2019-9-6
When I tried:
a = (fread(fid,'uint8')');
It shows error: Index exceeds the number of array elements (3529175).
As my project is a decoder where each block contains different number of bit.
And I need to read the input file bit by bit in order to execute the function in each block.
Walter Roberson
2019-9-6
bittable = (dec2bin(0:255, 8) - '0').';
a8 = fread(fid, 'uint8');
a = reshape( bittable(a8, :), [], 1);
Star Rats
2019-9-6
Hi @Walter, I tried but I get an error: Index in position 1 is invalid. Array indices must be positive integers or logical values.
Walter Roberson
2019-9-6
编辑:Walter Roberson
2019-9-6
a = reshape( bittable(double(a8)+1, :), [], 1);
Star Rats
2019-9-6
I tried and it causes error: Index in position 1 exceeds array bounds (must not exceed 8).
So I tried:
a = reshape( bittable(double(a8), :), [], 1);
and it causes the previous error: Index in position 1 is invalid. Array indices must be positive integers or logical values.
Star Rats
2019-9-6
Thanks @Walter. It runs without error. However, I wonder why it could not go into the loop of:
syncword = uint8([1 1 1 1 1 1 1 1 1 1 1 1 1 0 1]);
if(y(b:b+14) == syncword)
{
...
};
It could not scan if there exist the binary pattern of syncword.
Walter Roberson
2019-9-6
Your sync_word is wrong. It should be
syn = ones(1,12);
isMP2 = 1; %0 for MP4
layer = zeros(1,2);
sync_word = [syn, isMP2, layer];
Same total number of bits, but you had 0 1 for layer instead of 0 0.
Star Rats
2019-9-6
Hi @Walter, I think it is regardless of the syncword. It just couldn't go into the loop.
When I tested using the original coding, the if loop begins on the 1273th bit. But from the coding provided, it continues to scan the bit until the end without execute the if and for loop in between.
a = reshape( bittable(:,double(a8)+1), [], 1);
From the line above, is 'a' reading bit by bit or in an array form?
Walter Roberson
2019-9-6
It is reading byte by byte and translating to bits.
There is a theoretical possibility that the order of bits within a byte is reversed but it seems unlikely.
Star Rats
2019-9-12
Thanks @Walter, the code can be run but it does not improve the bit reading process. Is it possible to use the function GPUArray?
Walter Roberson
2019-9-12
编辑:Walter Roberson
2019-9-12
Yes, you can use gpu array. However it will slow down your code.
回答(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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)