Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc
- clear all
- % Define the data
- t0 = 0;
- tf = 8*pi;
- N = 1000;
- % 1. Sampled signal of info, named "m(t)"
- t = linspace(t0, tf, N);
- m = cos(t) + 0.5*t;
- figure(1);
- subplot(2,2,1);
- plot(t, m);
- title("1. Sampled signal m(t)");
- % 2. Quantization
- M = 5;
- numOfLevels = 2^M;
- quantizedAndLevels = quantization(m, numOfLevels);
- LEN_ALL = length(quantizedAndLevels);
- LEN1 = LEN_ALL - numOfLevels;
- quantized = quantizedAndLevels([1:LEN1]);
- levels = quantizedAndLevels([LEN1+1 : LEN_ALL]);
- subplot(2, 2, 2);
- plot(t, quantized);
- title(strcat("2. Quantized signal with ", num2str(numOfLevels), " levels"));
- % 3. Digitization - Info bitstream
- LEN = length(quantized);
- bitstream = zeros(1, M*LEN);
- for i = 1:LEN
- % For every qValue, I will add M digits 0,1 in bitsream list
- qValue = quantized(i);
- for j = 1:length(levels)
- level = levels(j);
- if qValue == level
- decimal = j-1;
- binary = decimalToBinary(decimal);
- binaryPad = pad(binary, M);
- % Binary variable is the M-digit word I will add in
- % my bitsream list
- for offset = 1:M
- bitstream(M *(i-1) + offset) = binaryPad(offset);
- end
- break
- end
- end
- end
- bitstream;
- % 4. Channel encoding
- X = 7; % I will send X 0's or 1's instead of a single 0 or 1
- encodedInfo = channelEncoding(bitstream, X);
- LEN = length(encodedInfo);
- subplot(2,2,3);
- plot(encodedInfo);
- title('Signal bitstream after encoding');
- subplot(2,2,4);
- OFFSET = 100;
- yAxis = encodedInfo([LEN/2-OFFSET : LEN/2+OFFSET]);
- plot(yAxis);
- title(strcat("Bistream zoomed (", num2str(2*OFFSET+1), " bits in center)"));
- bitString = prettyPrint(encodedInfo)
- % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- % Function 1 - Quantization
- function quantizedAndLevels = quantization(m, numOfLevels)
- MAX = max(m);
- MIN = min(m);
- LEVELS = linspace(MIN, MAX, numOfLevels+2);
- levels = zeros(1, numOfLevels);
- for i = 2:length(LEVELS)-1
- levels(i-1) = LEVELS(i);
- end
- % levels is done, now I have to quantize the values
- quantized = zeros(1, length(m));
- for i = 1:length(m)
- value = m(i);
- difference = 10^8;
- optimumLevel = -1;
- for j = 1:length(levels)
- level = levels(j);
- if abs(value - level) < difference
- difference = abs(value - level);
- optimumLevel = level;
- end
- end
- quantized(i) = optimumLevel;
- end
- quantizedAndLevels = [quantized levels];
- end
- % Function 2 - Decimal to binary
- function binary = decimalToBinary(decimal)
- sum = decimal;
- if decimal == 0
- binary = 0;
- else
- N = floor(log2(decimal) + 1);
- binary = zeros(1, N);
- for i = N-1: -1: 0
- if sum >= 2^i
- sum = sum - 2^i;
- binary(N - i) = 1;
- else
- binary(N - i) = 0;
- end
- end
- end
- end
- % Function 3 - Pad with zeros - Digitization
- function binaryPad = pad(binary, M)
- binaryPad = zeros(1:M);
- if length(binary) < M
- difference = M - length(binary);
- PAD = 1:difference;
- for i = 1:difference
- PAD(i) = 0;
- end
- binaryPad = [PAD binary];
- elseif length(binary) == M
- binaryPad = binary;
- else
- display('Cannot do that');
- end
- end
- % Function 4 - Channel encoding.
- % In this function I send X 0's or 1's instead of a single 0 or 1
- function encodedInfo = channelEncoding(bitstream, X)
- encodedInfo = zeros(1, X*length(bitstream));
- for i = 1:length(bitstream)
- BIT = bitstream(i);
- for j = 1:X
- encodedInfo(X*(i-1) + j) = BIT;
- end
- end
- encodedInfo;
- end
- % Function 5 - PrettyPrint of a bitstream (encodedInfo)
- function bitString = prettyPrint(bitstream)
- bitString = "";
- for i = 1:length(bitstream)
- BIT = bitstream(i);
- bitString = bitString + num2str(BIT);
- end
- bitString;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement