Skip to content
Snippets Groups Projects
Commit d1f48939 authored by Jack Driscoll's avatar Jack Driscoll
Browse files

matlab file for generating 12 bit noisy samples from msf input

parent e0d59823
No related branches found
No related tags found
No related merge requests found
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment