diff --git a/MSF/MSF_decoder/tb/matlab/noise_quantised_wave.m b/MSF/MSF_decoder/tb/matlab/noise_quantised_wave.m new file mode 100644 index 0000000000000000000000000000000000000000..d3138970d23a7c224e7219b45a923ce702de4cd3 --- /dev/null +++ b/MSF/MSF_decoder/tb/matlab/noise_quantised_wave.m @@ -0,0 +1,65 @@ +bit_res = 12; +sample_length = 120000; + +fid = fopen('msf_sample.csv', 'r'); +binary_sequence = fscanf(fid, '%1d').'; +binary_sequence = binary_sequence(:, 1:sample_length); + +twelve_bit_array = binary_sequence*((2^bit_res-1)-2^(bit_res-3))+(2^(bit_res-4))*ones(1, sample_length); + +twelve_bit_array = movmean(twelve_bit_array, 10); + +num_bits = length(twelve_bit_array); +original_time = 1:num_bits; + +high_res_time = linspace(1, num_bits, num_bits * 10); + +% Interpolate to create smooth transitions +%smooth_wave = interp1(original_time, twelve_bit_array, high_res_time, 'pchip'); + +% Add noise to the signal +noise_amplitude = 200; % Adjust the amplitude of the noise +noisy_wave = twelve_bit_array + noise_amplitude * randn(size(twelve_bit_array)); + +% Scale the noisy signal to ensure it's within the range of 12-bit values +noisy_wave = max(0, min(4095, noisy_wave)); + +% Quantize the noisy signal into 12-bit values +quantized_wave = round(noisy_wave); + +hex_wave = dec2hex(quantized_wave); + +writematrix(hex_wave, "adc_msf_input.csv"); + +% Convert the quantized values to binary strings +binary_wave = dec2bin(quantized_wave, 12); + +% Plot the original, noisy, and quantized signals +figure; +subplot(3,1,1); +stairs(original_time, twelve_bit_array, 'b', 'LineWidth', 1.5); +title('Original 12-bit Signal'); +xlabel('Time'); +ylabel('12-bit Value'); + +subplot(3,1,2); +plot(original_time, noisy_wave, 'r', 'LineWidth', 1.5); +title('Noisy and Smoothed Signal'); +xlabel('Time'); +ylabel('12-bit Value'); + +subplot(3,1,3); +stairs(original_time, quantized_wave, 'g', 'LineWidth', 1.5); +title('Quantized Signal'); +xlabel('Time'); +ylabel('12-bit Value'); + +% Display some results +disp('First 10 original 12-bit values:'); +disp(twelve_bit_array(1:10)); +disp('First 10 noisy 12-bit values:'); +disp(noisy_wave(1:10)); +disp('First 10 quantized 12-bit values:'); +disp(quantized_wave(1:10)); +disp('First 10 quantized 12-bit binary values:'); +disp(binary_wave(1:10, :)); \ No newline at end of file