Matrix dimensions must agree error
1 次查看(过去 30 天)
显示 更早的评论
Here is the code, and I suspect that the last batch of data is caused by last chunk remaining data. However, I am not sure how to handle it.
filename='rf.bin'; % filename='./rf.bin';
fid=fopen(filename,'r');
dataHeader=fread(fid,123,'uint8'); % skipping the header for .bin
NsperBatch = 1e3; % number of sample per batch
K=100; % Average every K set of values, K=100 in this case
magSpectrumMat=[];
while ~feof(fid)
magSpectrum=0;
for k=1:K
data=fread(fid,NsperBatch*2,'int16','b');
dataIQ=data(1:2:end)+1i*data(2:2:end);
dataSpectrum=fftshift(fft(dataIQ));
magSpectrum=magSpectrum+abs(dataSpectrum).^2;
end
magSpectrum = magSpectrum/K;
magSpectrumMat = [magSpectrumMat magSpectrum];
end
magSpectrumMat_dB=pow2db(magSpectrumMat);
0 个评论
采纳的回答
Walter Roberson
2017-10-11
You have
data=fread(fid,NsperBatch*2,'int16','b');
dataIQ=data(1:2:end)+1i*data(2:2:end);
The addition could fail if the fread() returns an odd number of values (but at least 3) instead of the expected even number of values. That could happen if the fread() encountered end of file.
If fread returned 1 value, then the addition would be of one value plus an empty vector, which would give an empty vector as a result, which is not an error. dataSpectrum would then involve the fft of an empty vector, which is not an error, giving an empty result. magSpectrum=magSpectrum+abs(dataSpectrum).^2; where the dataSpectrum is empty would give an empty right hand side, resulting in an empty result. Nothing would go wrong syntactically -- but it probably is not the result you would have preferred.
If fread returns empty, then the results are the same as just described.
fread can return empty because feof() does not always predict end of file -- that is, feof() does not always know that all of the data has been read and the next read will encounter end of file. In the context you use it for, feof() should predict end of file because of a non-standard extension MATLAB made to their I/O system, but it is safest not to rely on it.
I would recommend that you test the length() of data before using it. If you read in a partial block of K groups of data, then you will need to decide whether to ignore the entire group or to process what you got. If you decide to process what you got then you probably want to change the division by K in magSpectrum.
You could also consider vectorizing the loop:
data = fread(fid, [NsperBatch*2, K], 'int16', 'b'); %read down columns
dataIQ = complex(data(1:2:end,:), data(2:2:end,:));
dataSpectrum = fftshift(fft(dataIQ)); %performed down columns
magSpectrum = mean(abs(dataSpectrum).^2, 2);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!