From: Jon on
I need to read in a binary file with real (in phase) and complex (quadrature) values of 4 bytes each. Is there a quicker or more memory efficient way of reading them in and converting to real than this? I need to read in very large files and I'm running out of memory before this completes so I'll probably have to also add while loops anyhow....

fseek(fid,0,'eof');
filesize = ftell(fid);
frewind(fid);

real = fread(fid, filesize-4, 'single', 4);
real = sqrt(2)*real*cos(alpha*period*(1:(filesize/8)))*multiplier;

fseek(fid, 4, 'bof');

imag = fread(fid, filesize, 'single', 4);
imag = sqrt(2)*imag*sin(alpha*period*(1:(filesize/8)))*multiplier;

wavsamples = real - imag
From: Walter Roberson on
Jon wrote:

> real = fread(fid, filesize-4, 'single', 4);

Please do not use variable names that are the same as the names as
Matlab built-in functions: it is going to confuse any readers, and if
you get into the habit you are inevitably going to end up confusing
Matlab and getting yourself a bug that is hard to find.
From: Jon on
I changed one line
realpart = fread(fid, filesize-4, 'single', 4);
realpart = sqrt(2)*real*cos(alpha*period*(1:(filesize/8)))*multiplier;

fseek(fid, 4, 'bof');

imagpart = fread(fid, filesize, 'single', 4);
wavesample = realpart - sqrt(2)*imag*sin(alpha*period*(1:(filesize/8)))*multiplier;

I don't know if that makes a difference or not. Anyone know if matlab could do something like:

realpart = sqrt(2)*fread(fid, filesize-4, 'single', 4)*cos(alpha*period*(1:(filesize/8)))*multiplier;

fseek(fid, 4, 'bof');

wavesample = realpart - sqrt(2)*fread(fid, filesize, 'single', 4)*sin(alpha*period*(1:(filesize/8)))*multiplier;

That should be less memory intensive right?

Walter Roberson <roberson(a)hushmail.com> wrote in message <J_qPn.23491$%u7.19078(a)newsfe14.iad>...
> Jon wrote:
>
> > real = fread(fid, filesize-4, 'single', 4);
>
> Please do not use variable names that are the same as the names as
> Matlab built-in functions: it is going to confuse any readers, and if
> you get into the habit you are inevitably going to end up confusing
> Matlab and getting yourself a bug that is hard to find.
From: Walter Roberson on
Jon wrote:
> Anyone know if matlab
> could do something like:
>
> realpart = sqrt(2)*fread(fid, filesize-4, 'single',
> 4)*cos(alpha*period*(1:(filesize/8)))*multiplier;
>
> fseek(fid, 4, 'bof');
>
> wavesample = realpart - sqrt(2)*fread(fid, filesize, 'single',
> 4)*sin(alpha*period*(1:(filesize/8)))*multiplier;

Not quite but close. Your count should not be filesize-4 but should instead be
filesize/8 with no subtractions. The count for fread is the count of the
number of _items_ to be read, not the count of the number of _bytes_ to be read.
From: Jon on
Thanks for the help. So you can multiply an fread that results in a vector with a vector? I should probably specify the multiplication as element by element. I suppose I need to put this in a while loop so I can actually see the output before it runs out of memory.

realpart = sqrt(2)*fread(fid, filesize/8, 'single', 4) .* cos(alpha*period*(1:(filesize/8)))*multiplier;

fseek(fid, 4, 'bof');

wavesample = realpart - sqrt(2)*fread(fid, filesize/8, 'single', 4) .* sin(alpha*period*(1:(filesize/8)))*multiplier;

Walter Roberson <roberson(a)hushmail.com> wrote in message <humjou$o99$1(a)canopus.cc.umanitoba.ca>...
> Jon wrote:
> > Anyone know if matlab
> > could do something like:
> >
> > realpart = sqrt(2)*fread(fid, filesize-4, 'single',
> > 4)*cos(alpha*period*(1:(filesize/8)))*multiplier;
> >
> > fseek(fid, 4, 'bof');
> >
> > wavesample = realpart - sqrt(2)*fread(fid, filesize, 'single',
> > 4)*sin(alpha*period*(1:(filesize/8)))*multiplier;
>
> Not quite but close. Your count should not be filesize-4 but should instead be
> filesize/8 with no subtractions. The count for fread is the count of the
> number of _items_ to be read, not the count of the number of _bytes_ to be read.