0% found this document useful (0 votes)
19 views16 pages

Analog Signal Processing Guide

The document describes code for various steps in digital communication - analog to digital conversion, source encoding, channel encoding and adding noise. It includes code to sample and quantize an analog signal, encode it with PCM and channel encode with a Hamming code. It also includes code to add noise to a signal and filter it before demodulation.

Uploaded by

Khushi Baurasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views16 pages

Analog Signal Processing Guide

The document describes code for various steps in digital communication - analog to digital conversion, source encoding, channel encoding and adding noise. It includes code to sample and quantize an analog signal, encode it with PCM and channel encode with a Hamming code. It also includes code to add noise to a signal and filter it before demodulation.

Uploaded by

Khushi Baurasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

PART -1

An analog signal is to be transmitted over a noisy channel and received at the receiver
Part1 (Q1). Analog to Digital converter.
- Choose the sampling rate, Quantization levels and encoding with suitable reasons .
Ans – The sampling rate used is - 4 , Quantization levels = 2^4 = 8 ;
* Code of Analog To Digital Converter -
Sr No. Code Comments
1. close all
2. clear all
3. clc
4. Fs = 200; Sampling frequency
5. T = 1/Fs; Sampling Period
6. t = 0:T:1; Time vector from 0 to 1
second
7. f = 10; Frequency of the analog
signal
8. x = sin(2*pi*f*t); Analog signal
9.
10. % Plotting the original analog signal
11. subplot(4,1,1);
12. plot(t, x);
13. xlabel('Time (s)');
14. ylabel('Amplitude');
15. title('Analog Signal');
16.
17. % Sampling
18. subplot(4,1,2);
19. stem(t, x, 'r', 'linewidth', 1.5);
20. xlabel('Time (s)');
21. ylabel('Amplitude');
22. title('Sampled Signal');
23.
24. % Quantization
25. bits = 4; Number of bits for
quantization
26. levels = 2^bits; Number of quantization
levels
27. quantized_signal = round((x + 1) * (levels - 1) / Quantize the analog signal
2);
28.
29. % Plotting the quantized signal
30. subplot(4,1,3);
31. stem(t, quantized_signal, 'b', 'linewidth', 1.5);
32. xlabel('Time (s)');
33. ylabel('Quantized Value');
34. title('Quantized Signal');
35.
36. % ADC Output
37. adc_output = zeros(size(quantized_signal));
38. for i = 1:length(quantized_signal)
39. adc_output(i) = quantized_signal(i) / (levels -
1);
40. end
41.
42. % Plotting the ADC output
43. subplot(4,1,4);
44. stairs(t, adc_output, 'g', 'linewidth', 1.5);
45. xlabel('Time (s)');
46. ylabel('ADC Output');
47. title('ADC Output');
48.
49. % Adjusting plot appearance
50. set(gcf, 'Position', [100, 100, 800, 600]);
51. subplot(4,1,1);
52. axis tight;
53.
54. % Displaying the figure
55. figure;
56.
57. % Plotting the original analog signal for
reference
58. plot(t, x, 'r', 'linewidth', 1.5);
59. xlabel('Time (s)');
60. ylabel('Amplitude');
61. title('Analog Signal (Reference)');
62. axis tight;

* Output Waveform of Analog To Digital Converter


Part1 (Q2). Code of Source Encoder –
The Encoding Scheme used is Pulse Code Modulation -
Sr. No. Code Comments
1. Close all
2. Clear all
3. clc
4. % Generate a continuous analog signal
5. Fs = 100; Sampling frequency
6. t = 0:1/Fs:1 ; Time vector
7. f = 5; Frequency of the signal
8. analog_signal = sin(2*pi*f*t); Signal
9.
10. % PCM Encoder
11. num_bits = 8; Number of bits for each
sample
12. quantized_signal = round(analog_signal *
(2^(num_bits-1)));
13.
14. % Display original and quantized signals
15. figure;
16. subplot(2,1,1);
15. plot(t, analog_signal);
16. title('Original Analog Signal');
17.
18. subplot(2,1,2);
19. stem(t, quantized_signal);
20. title('Quantized Signal');
21. xlabel('Time (s)');
22. ylabel('Quantized Value');

 Output Waveform of Source Encoder –

Part1 (Q3). Code of Channel Encoder –


Sr. No. Code Comments
1. Close all
2. Clear all
3. clc Ans-
4. % Parameters Channel
5. n = 7; Total number of bits in a encoding is
codeword typically used to
6. k = 4; Number of information add redundancy
bits to the
7. message = randi([0, 1], 1, k); Generate random k-bit transmitted
message signal to aid in
8. error
9. % Channel encoding (Hamming code)
10. encoded_message = encode(message, n, k,
'hamming/binary'); detection and
11.
correction at the
12. % Display the original message and encoded
receiver . One
message
common
13. figure ;
14. subplot(2,2,2) approach is to
15. plot(message); Plotting original signal use error-
16. title('Original Message'); correcting
15. figure ; codes,
16. subplot(2,2,2) such
17. plot(encoded_message); Plotting encoded signal as Hamming
18. title('Encoded message'); codes . In this
19.
example ,demonstrating a simple (7,4) Hamming code for channel

encoding .
 Output Waveform of Channel Encoder

Part1.(Q4) Code for Channel-


Sr. No. Code Comments
1. Close all
2. Clear all
3. clc
4. % Parameters
5. Fs = 1000; Sampling frequency
6. f = 10; Signal frequency
7. t = 0:1/Fs:1; Time vector
8.
9. % Generate analog signal (sine wave)
10. analog_signal = sin(2*pi*f*t);
11.
12. % Plot the analog signal
13. figure;
14. plot(t, analog_signal);
15. title('Analog Signal');
16. xlabel('Time');
15. ylabel('Amplitude');
16. % Parameters for noise
17. SNR_dB = 20; Signal-to-noise ratio
in dB
18. noise_power = var(analog_signal) / Noise Power
(10^(SNR_dB/10));
19.
20. % Generate Gaussian noise
21. noise = sqrt(noise_power) *
randn(size(analog_signal));
22.
23. % Add noise to the signal
24. noisy_signal = analog_signal + noise;
25.
26. % Plot the noisy signal
27. figure;
28. plot(t, noisy_signal);
29. title('Noisy Signal');
30. xlabel('Time');
31. ylabel('Amplitude');
32. % Design a simple low-pass filter
33. cutoff_frequency = 20 ; Adjust as needed
34. order = 4; Filter order
35.
36. % Design the filter
37. [b, a] = butter(order, cutoff_frequency / (Fs / 2));
38.
39. % Apply the filter to the received signal
40. filtered_signal = filter(b, a, noisy_signal);
41.
42. % Plot the filtered signal
43. figure;
44. plot(t, filtered_signal);
45. title('Filtered Signal');
46. xlabel('Time');
47. ylabel('Amplitude');
 Output of Channel
Sr. Code Comments
No.
1. close all
2 clear all

3 clc
4 pkg load communications
5 M=8; No of levels
6 numsymbols=10000; No of symbols to be generated
7 bits=randi([0 M-1],1,numsymbols); random bit sequence
modulated_signal=pskmod(bits,M,pi/M); phase modulating the given bit
sequence
modulated_signal=awgn(modulated_signal,15,'measured'); adding white gaussian noise
demodulatedbits=pskdemod(modulated_signal,M,pi/M); demodulating the psk signal
incorrectsymbols=find(demodulatedbits~=bits) finding incorrect symbols in
demodulated data
scatterplot(modulated_signal) plotting constellation diagram
of modulated signal

scatterplot(modulated_signal);
hold on;
scatter(real(modulated_signal(incorrectsymbols)),
imag(modulated_signal(incorrectsymbols)),'r','filled');
%plotting constellation diagram of modulated signal and
incorrect data
ber=length(incorrectsymbols)/length(bits); Finding bit error rate

Part1 (Code 2 )
Part1 (Q5.) *Code for Channel Decoder
Sr. No. Code Comments
1. Close all
2. Clear all
3. clc
4. function decoded_data = * Output of
hamming_decoder(received_data) Channel
5. % Hamming Decoder (7,4) Decoder
6.
7. % Define the parity check matrix for (7,4) Hamming
code
8. H=[
9. 1 1 0 1 1 0 0;
10. 0 1 1 1 0 1 0;
11. 1 1 1 0 0 0 1;
12. ];
13.
14. % Split the received data into 7-bit blocks
15. received_blocks = reshape(received_data, 7,
length(received_data)/7)';
16.
15. % Initialize the decoded data matrix
16. decoded_data = [];
17.
18. % Decode each 7-bit block
19. for i = 1:size(received_blocks, 1)
20. received_block = received_blocks(i, :)';
21. syndrome = mod(received_block * H', 2);
22.
23. % Correct errors, if any
24. if sum(syndrome) > 0
25. error_position = bin2dec(num2str(syndrome'));
26. received_block(error_position) =
mod(received_block(error_position) + 1, 2);
27. end
28.
29. % Extract the original 4 bits
30. decoded_data = [decoded_data;
received_block(1:4)];
31. end
32.
33. % Convert the decoded data to a row vector
34. decoded_data = 'decoded_data';
35. end
36.
37. % Example Usage
38. received_data = [1 0 1 0 0 1 0 1 1 1 0 1 0 1 1 0 1 0 1 0 1 1 Received data
1 1 0 0 0 1 0 1 1 1 1 0 0 1 0];
39. decoded_output = hamming_decoder(received_data);
40.
41. % Display results
42. figure ;
43. subplot(2,1,1) ;
44. plot(received_data); Plotting
received data
45. title('Received Data');
Part1(Q6.) Code of SourceDecoder
Sr. No. Code Comments
1. Close all
2. Clear all
3. clc
4. % Generate a continuous analog signal
5. Fs = 100; Sampling Frequency
6. t = 0:1/Fs:1 ; Time Vector
7. f = 5; Siganl Frequency
8. analog_signal = sin(2*pi*f*t);
9.
10. % PCM Encoder
11. num_bits = 8;
12. quantized_signal = round(analog_signal * Defined Function
(2^(num_bits-1)));
13.
14. % Display original and quantized signals
15. figure;
16. subplot(2,1,1);
15. plot(t, analog_signal); Plotting analog signal
16. title('Original Analog Signal');
17.
18. subplot(2,1,2);
19. stem(t, quantized_signal);
20. title('Quantized Signal');
21. xlabel('Time (s)');
22. ylabel('Quantized Value');
23.
24. % Assume received PCM signal (after transmission over
a noisy channel)
25. received_pcm_signal =
quantized_signal(round(analog_signal * (2^(num_bits-
1))));
26. % Replace ... with your received PCM signal
27.
28. % Demodulate the PCM signal
29. demodulated_signal =
pcmdecode(received_pcm_signal);
30.
31. % Plot the demodulated signal
32. figure;
33. plot(demodulated_signal);
34. title('Demodulated Signal');
35. xlabel('Sample Number');
36. ylabel(‘Amplitude’)

 Output waveform of Source Decoder


Sr. No. Code Comments
1. Close all
2. Clear all
3. clc
4. % Parameters
5. Fs = 200; Sampling
frequency
6. f = 10; Signal frequency
7. t = 0:1/Fs:1; Time vector
8.
9. % Generate an example analog signal (sine wave)
10. analog_signal = sin(2*pi*f*t);
11.
12. % Quantize the analog signal to PCM
13. num_bits = 8; % Number of bits for quantization
14. quantized_signal = round(analog_signal * (2^(num_bits-
1) - 1));
15.
16. % Digital-to-Analog Conversion using Interpolation
15. reconstructed_analog_signal =
interp1(1:length(quantized_signal), quantized_signal,
linspace(1, length(quantized_signal), length(t)),
'previous');
16.
17. % Plot the original analog signal, quantized PCM signal,
and reconstructed analog signal
18. figure;
19.
20. subplot(3, 1, 1);
21. plot(t, analog_signal);
22. title('Original Analog Signal');
23. xlabel('Time');
24. ylabel('Amplitude');
25.
26. subplot(3, 1, 2);
27. stem(quantized_signal);
28. title('Quantized PCM Signal');
29. xlabel('Sample Number');
30. ylabel('Amplitude');
31.
32. subplot(3, 1, 3);
33. plot(t, reconstructed_analog_signal);
34. title('Reconstructed Analog Signal from PCM');
35. xlabel('Time');
36. ylabel('Amplitude');
Part1(Q7.) Code of Digital to Analog Converter
 Output Waveform for Digital To Analog Converter

You might also like