Need help generating turbocode data with awgn noise in matlab for training my ML model.
18 views (last 30 days)
Show older comments
I previously managed to figure out the generation of code without noise which worked with training of my model, but now I need to add awgn noise for further research, but am facing trouble in using the awgn library. I am getting non binary and "/" characters in the output when I try generate the output using awgn.
Can I please get some pointers on how to generate such codes and what I am doing wrong?
my current code:
% Define the parameters for your Turbo code
blockLength = 678; % Block length (adjust as needed)
bitRates = [1/2, 1/3, 1/4, 1/6]; % Set of available bit rates
constraintLength = 4; % Constraint length
numSamples = 1000; % Number of samples to generate
snr = 10; % Signal-to-noise ratio
% Initialize a cell array to store the encoded strings
encodedStrings = cell(1, numSamples);
% Define the desired length of the encoded strings (2048)
desiredLength = 2048;
% Loop to generate encoded strings
for sample = 1:numSamples
% Randomly choose one of the available bit rates
selectedBitRate = bitRates(randi(length(bitRates)));
% Create a TurboEncoder object with the compatible trellis structure
trellis = poly2trellis(constraintLength, [13 15]);
% Calculate the required interleaver indices based on block length
interleaverIndices = randperm(blockLength);
% Create a TurboEncoder object with the correct trellis structure and interleaver indices
turboEncoder = comm.TurboEncoder('TrellisStructure', trellis, 'InterleaverIndices', interleaverIndices);
% Generate some random input data (binary) with the same block length
inputData = randi([0 1], 1, blockLength);
% Encode data with the TurboEncoder
encodedData = turboEncoder(inputData');
% Add AWGN noise
noisyData = awgn(encodedData, snr, 'measured');
% Round the noisy data to the nearest integer (0 or 1)
roundedData = round(noisyData);
% Convert the encoded bits into a binary string
binaryString = char('0' + roundedData');
% Ensure the binary string is of the desired length by adding padding bits on the right
if length(binaryString) < desiredLength
% Calculate the number of padding bits needed
numPaddingBits = desiredLength - length(binaryString);
% Generate padding bits (zeros)
paddingBits = repmat('0', 1, numPaddingBits);
% Concatenate padding bits to the binary string
paddedString = [binaryString, paddingBits];
else
% Trim to the desired length
paddedString = binaryString(1:desiredLength);
end
encodedStrings{sample} = paddedString;
% Display progress (optional)
if mod(sample, 1000) == 0
disp(['Generated ', num2str(sample), ' encoded strings...']);
end
end
% Combine the encoded strings into a single line with spaces
encodedLine = strjoin(encodedStrings, ' ');
% Define the output file name
outputFileName = 'encoded_noisy_output_fin.txt';
% Write the encoded line to a text file
fid = fopen(outputFileName, 'wt');
fprintf(fid, '%s', string(encodedLine));
fclose(fid);
disp(['Encoded data for ', num2str(numSamples), ' samples has been saved to "', outputFileName, '".']);
2 Comments
Image Analyst
on 8 Dec 2023
We don't want to go out to some third party website when you can just as easily attach your m-file here for all of us to use immediately. Make it easy for us to help you, not hard.
Answers (1)
Gyan Vaibhav
on 21 Dec 2023
Edited: Gyan Vaibhav
on 21 Dec 2023
Hi Shrestha,
I understand that you are trying to generate noisy binary strings and then adding AWGN (Additive White Gaussian Noise) to simulate a noisy communication channel.
Upon reviewing the code, the following are the causes for the unexpected behaviour encountered. Here are a few pointers to help understand and rectify the errors.
- AWGN and Binary Data: Introducing AWGN to binary data inherently results in noisy data that is not strictly binary, as the noise can cause deviations from the original binary values of 0 and 1.
- Rounding to Nearest Integer: The code is rounding the noisy data to the nearest integer to restore it to a binary format, which is a typical method used in simulating binary data transmission through a noisy channel. However, upon inspecting the “roundedData” in the workspace, there are aberrant values such as -1 and 2, instead of the anticipated binary values of 0 or 1.
- Character Conversion: The conversion of rounded binary data into a character array is performed using “char('0' + roundedData')”. This conversion would be accurate in this case only if “roundedData” comprises solely 0s and 1s. Any deviation due to rounding errors in the process can result in unexpected characters. The “char” function works based on the ASCII character table. According to the ASCII table, the character preceding "0" is "/", which aligns with the observation that “char('0' + (- 1))” is converted to "/".
To address these issues, consider the following solutions:
- Clamping the values: If it is required to clip the values greater than 1 to 1 and the values smaller than 0 to 0, you can do this by including the following line in the code just before rounding it.
% Limit the noisy data to the range [0, 1]
limitedData = max(min(noisyData, 1), 0);
In the snippet above, “min(noisyData, 1)” ensures that all values are at most 1, and “max(..., 0)” ensures that all values are at least 0. This guarantees that after limiting, all values are within the range [0, 1], and subsequent rounding will only produce 0 or 1.
- Alternative AWGN Implementation: Consider using a different function or method to add AWGN to your data.
Consider going through these MathWorks documentation pages for more information on:
- Character Array: https://www.mathworks.com/help/matlab/ref/char.html
- Rounding to nearest decimal or integer: https://www.mathworks.com/help/matlab/ref/round.html
- Add White Gaussian Noise: https://www.mathworks.com/help/comm/ref/awgn.html
Hope this helps.
Thanks
Gyan
0 Comments
See Also
Categories
Find more on Propagation and Channel Models in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!