from
Manchester to standard logic decoder
by Robert
Converts a manchester encoded string to its standard logic equivalent.
|
| manchester2bin(inputData)
|
function decodedData = manchester2bin(inputData)
% MANCHESTER2BIN(inputData) decodes a manchester serial
% sequence to its corresponding binary value.
%
% Decoding is done according to G.E. Thomas' convention
% (high-to-low '01' = '1', low-to-high '10'= 0), assuming LSB first
% transmission.
%
% Syntax: decodedData = MANCHESTER2BIN(inputData)
%
% Where:
% inputData = string with binary values which length must be EVEN,
% with the MSB at at the left-most position (index = 1)
% decodedData = string with binary values which length is half of
% inputData with the MSB at the left-most position
%
% If the inputData sequence is not a valid manchester code MANCHESTER2BIN
% will return an empty string.
%
% Example:
% >>decodedData = manchester2bin('01011010')
%
% decodedData =
% 1100
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% Author: Robert Brookhuis
% email: r.a.brookhuis(at)gmail.com
% MAR 2009; Last revision: 02-Mar-2009
nBits = length(inputData); % length of inputData
% preallocate decodedData as array of doubles, for speed. Faster than
% handling strings. Array is converted to string after decoding.
decodedData = ones(1,nBits/2);
if mod(nBits,2)~=0 % check if array is even, otherwise generate error
error('Length of array must be even')
end
for i = nBits:-2:2 % count from max. size downwards with steps of 2
if inputData(i) ~= inputData(i-1) % if bits are unequal
decodedData(i/2) = inputData(i); % the first bit is the binary value
else
decodedData = []; %if bits are equal, it's not manchester code
break %exit for loop
end
end
decodedData = char(decodedData); %cast decodedData back to string
|
|
Contact us at files@mathworks.com