vectorized octave

octave can be very fast when vectorized, particularly when reading and formatting streams. Here’s an example which takes an array read from stdin and reformats :




	%reapportion  bytes
	
	[m n]=size(input);

	if (en=='ieee-be')
	
	%7 4 byte numbers
	outputA = reshape(input(:,1:14),m,2,7);
	outputB = reshape((bitshift(outputA(:,1,:),16) + outputA(:,2,:)),m,7);

	%4 2 byte numbers
	%8 4 byte numbers
	outputA = reshape(input(:,19:34),m,2,8);
	outputC = reshape((bitshift(outputA(:,1,:),16) + outputA(:,2,:)),m,8);
	
	%2 2 byte numbers
	%4 4 byte numbers
	outputA = reshape(input(:,37:44),m,2,4);
	outputD = reshape((bitshift(outputA(:,1,:),16) + outputA(:,2,:)),m,4);

	else

	%7 4 byte numbers
	outputA = reshape(input(:,1:14),m,2,7);
	outputB = reshape((outputA(:,1,:) + bitshift(outputA(:,2,:),16)),m,7);

	%4 2 byte numbers
	%8 4 byte numbers
	outputA = reshape(input(:,19:34),m,2,8);
	outputC = reshape((outputA(:,1,:) + bitshift(outputA(:,2,:),16)),m,8);
	
	%2 2 byte numbers
	%4 4 byte numbers
	outputA = reshape(input(:,37:44),m,2,4);
	outputD = reshape((outputA(:,1,:) + bitshift(outputA(:,2,:),16)),m,4);

	end

	%must recast for signs
	output = [int32(outputB) int16(input(:,15:18)) int32(outputC) int16(input(:,35:36)) int32(outputD) int16(input(:,45:n))]; 	
	
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s