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

AM, FM, BPSK, QPSK, and QAM Modulation

The document contains Matlab code for generating and plotting various digital communication signals including AM, FM, BPSK, PSK, QAM, and DSBSC. It discusses generating the signals, plotting them, taking the FFT, and in some cases transmitting and receiving the signals. The codes generate carrier signals, modulate the carrier with the message signal using various modulation techniques, optionally filter and transmit the modulated signal, and at the receiver section demodulate and decode the signal to retrieve the original message.

Uploaded by

Prabhakar Das
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 views12 pages

AM, FM, BPSK, QPSK, and QAM Modulation

The document contains Matlab code for generating and plotting various digital communication signals including AM, FM, BPSK, PSK, QAM, and DSBSC. It discusses generating the signals, plotting them, taking the FFT, and in some cases transmitting and receiving the signals. The codes generate carrier signals, modulate the carrier with the message signal using various modulation techniques, optionally filter and transmit the modulated signal, and at the receiver section demodulate and decode the signal to retrieve the original message.

Uploaded by

Prabhakar Das
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

Day 4: Communication 1.

Generation of AM signal with carrier and plot its FFT Matlab code:
clc; close all; clear all; fm=100; fc=10000; fs=2*fc; t=0:(1/fs):(1/fm); m=2.*cos(2*pi*fm.*t); c=10.*cos(2*pi*fc.*t); func=ones(1,length(m))+(.5/2).*cos(2*pi*fm.*t) st=func.*cos(2*pi*fc.*t); plot(t,m); figure plot(t,c) figure plot(t,st)

2. Generation of AM signal without carrier and plot its FFT Matlab code:
clc; close all; clear all; fm=100; fc=10000; fs=fc*5; noc=100; ts=1/fs; t=0:ts:1/fm m=sin(2*pi*fm*t); c=sin(2*pi*fc*t); plot(t,m) figure plot(t,c); ds_mod=m.*c; plot(t,ds_mod);

3. Generate FM wave and plot fft Matlab code:


clc clear all close all fm=25 fc=400 b=10 t=0:0.0001:0.1; m=sin(2*pi*fm*t); figure plot(t,m) c=sin(2*pi*fc*t); figure plot(t,c) st=sin(2*pi*fc*t+(b.*sin(2*pi*fm*t))); plot(t,st)

4. Binary Phase shift keying (BPSK): Transmitter and its FFT Matlab code:
clc clear all close all %Modulation of the sine wave signal with the random bit stream %parameters

f=10e6; fs=10*f; data_rate=1e6; no_of_cycles=f/data_rate; ts=1/fs; t=0:ts:no_of_cycles*fs/f*ts; sig_0=sin(2*pi*f*t); sig_1=sin(2*pi*f*t+pi); len_sig_0=length(sig_0); sig_start=1; sig_end=sig_start+length(sig_0); for index=1:100 if (rand < 0.5) bit_stream(index)='0' BPSK_sig(sig_start:sig_end-1)=sig_0; else bit_stream(index)='1' BPSK_sig(sig_start:sig_end-1)=sig_1; end sig_start=sig_start+len_sig_0; sig_end=sig_end+len_sig_0; end %end of for loop plot(BPSK_sig) fft_sig=fftshift(fft(BPSK_sig)); mag_fft_sig=abs(fft_sig); db_mag_fft_sig=20*log10(mag_fft_sig); faxis=linspace(-1/2,1/2,length(fft_sig)); maxi=max(db_mag_fft_sig); norm_db_mag_fft_sig=db_mag_fft_sig-maxi; [junk,index1]=find(norm_db_mag_fft_sig<-40); norm_db_mag_fft_sig(index1)=-40; figure plot(faxis,norm_db_mag_fft_sig) %********************************************* %To generate filter using rectangular window %******************************************** W1=window(@rectwin,45) F1= fir1(44, .11*pi, W1) F_F1=fft(F1,512); F_FS1=fftshift(F_F1); AB1=abs(F_FS1); db_AB1=20*log10(AB1); faxis=linspace(-1,1,512); figure plot(faxis,db_AB1) filt_bpsk_sig=conv(F1,BPSK_sig);

fft_sig=fftshift(fft(filt_bpsk_sig)); mag_fft_sig=abs(fft_sig); db_mag_fft_sig=20*log10(mag_fft_sig); faxis=linspace(-1/2,1/2,length(fft_sig)); maxi=max(db_mag_fft_sig);

norm_db_mag_fft_sig=db_mag_fft_sig-maxi; [junk,index1]=find(norm_db_mag_fft_sig<-40); norm_db_mag_fft_sig(index1)=-40; figure plot(faxis,norm_db_mag_fft_sig) figure plot(filt_bpsk_sig) save filt_bpsk_sig filt_bpsk_sig bit_stream

5. ASK modulation 6. QPSK transmitter 7. Filtering using DSP filters, concept of channel 8. Binary Phase shift keying (BPSK): Receiver
clc clear all close all % Status of the Program % The Program is able to decode a BPSK bit stream successfuly evenafter % filtering. But the following issues still to need to be resolved % 1. Carrier Recovery, Here we have done it manually. We have to try % doing it using the fft command or any other technique % 2. Timing Recovery, Here we are doing it manually by observing the plot of the received signal % BPSK Receiver load filt_bpsk_sig fc=10e6; fs=10*fc; ts=1/fs; no_of_cycl=10; t=0:ts:ts*no_of_cycl*fs/fc; LO=sin(2*pi*fc*t); len_LO=length(LO); %for ind=1:len_LO:len cleaning_pt=25; % From observation of the received signal's plot x(1:cleaning_pt)=[]; len=len-cleaning_pt; remain=rem(len,len_LO) x(end-remain+1:end)=[]; x_mat=reshape(x,len_LO,[]); x_mat=x_mat'; LO_mat=repmat(LO,size(x_mat,1),1); prod_sigs=x_mat.*LO_mat; int_prod_sigs=sum(prod_sigs,2); x_coord=10*ones(1,length(int_prod_sigs)); stem(x_coord,int_prod_sigs); int_prod_sigs(int_prod_sigs>0)=1; int_prod_sigs(int_prod_sigs<0)=0; int_prod_sigs=int2str(int_prod_sigs);% Convert to character for comparing with bit stream decoded_bit_stream=int_prod_sigs'; % Just converting to column vector to comapare with bit_stream which is a column vector

% Check whether the decoded bit stream is equal to the sent bit stream bit_stream==decoded_bit_stream

9. 8 psk transmitter
clc clear all close all %generation of a random bit stream and a sine wave carrier and its FSK %modulation %*************************************** %*************************************** %GENERATION OF THE CARRIERS %*************************************** %*************************************** %INITIALISATION f2=10e5; data_rate=1e5; no_of_cycles=f2/data_rate/3; fs=10*f2; ts=1/fs; t=0:ts:fs/f2*no_of_cycles*ts; %================================================ % GENERATION OF THE CARRIERS FOR THE BIT DURATION %================================================ two_pi=2*pi; s0=sin(2*pi*f2*t); plot(s0) s1=sin(2*pi*f2*t+two_pi/8); % plot(sig_1) s2=sin(2*pi*f2*t+2*two_pi/8); % plot(sig_2) s3=sin(2*pi*f2*t+3*two_pi/8); % plot(sig_3) s4=sin(2*pi*f2*t+4*two_pi/8); % plot(sig_4) s5=sin(2*pi*f2*t+5*two_pi/8); % plot(sig_5) s6=sin(2*pi*f2*t+6*two_pi/8); % plot(sig_6) s7=sin(2*pi*f2*t+7*two_pi/8); % plot(sig_7) Tb=length(t); %*************************************** % 8 PSK %*************************************** sigstart=1; sigend=Tb; mod_sig=0; for no_of_bits=1:1:1000 symb=[]; for random_no_stream =1:1:3 if rand > 0.5 bit='1';

symb=[symb bit]; else bit='0'; symb=[symb bit]; end % end of if loop end %end of 1st for loop for bit to symbol mapping switch symb case '000' signal=s0; case '001' signal=s1; case '010' signal=s2; case '011' signal=s3; case '100' signal=s4; case '101' signal=s5; case '110' signal=s6; case '111' signal=s7; end mod_sig(sigstart:sigend)=signal; sigstart=sigend+1; sigend=sigend+Tb; end %end of 2nd for loop for bit generation

% fft_sig=fftshift(fft(mod_sig)); % mag_fft_sig=abs(fft_sig); % db_mag_fft_sig=10*log10(mag_fft_sig); % faxis=linspace(-fs/2,fs/2,length(fft_sig)); % maxi=max(db_mag_fft_sig); % norm_db_mag_fft_sig=db_mag_fft_sig-maxi; % plot(faxis,norm_db_mag_fft_sig) save psk_8 mod_sig s0 s1 s2 s3 s4 s5 s6 s7

10. 8 psk receiver


clc clear all close all %psk receiver% load psk_8 f2=10e5; data_rate=1e5; no_of_cycles=f2/data_rate/3; fs=10*f2; ts=1/fs; t=0:ts:fs/f2*no_of_cycles*ts; x=mod_sig; len=length(x);

%================================================ % GENERATION OF THE CARRIERS FOR THE BIT DURATION %================================================ LO=sin(2*pi*f2*t); LO_Q=sin(2*pi*f2*t+pi/2); len_LO=length(LO); cleaning_pt=0; % From observation of the received signal's plot x(1:cleaning_pt)=[]; len=len-cleaning_pt; remain=rem(len,len_LO) x(end-remain+1:end)=[]; x_mat=reshape(x,len_LO,[]); x_mat=x_mat'; I_mat=repmat(LO,size(x_mat,1),1); Q_mat=repmat(LO_Q,size(x_mat,1),1); prod_sigs=x_mat.*I_mat; int_prod_sigs_I=sum(prod_sigs,2); prod_sigs=x_mat.*Q_mat; int_prod_sigs_Q=sum(prod_sigs,2); plot(int_prod_sigs_I,int_prod_sigs_Q,'*')

% stem(int_prod_sigs); % figure % % hist(int_prod_sigs) % int_prod_sigs(int_prod_sigs>0)=1; % int_prod_sigs(int_prod_sigs<0)=0; % int_prod_sigs=int2str(int_prod_sigs);% Convert to character for comparing with bit stream % decoded_bit_stream=int_prod_sigs'; % Just converting to column vector to comapare with bit_stream which is a column vector % % Check whether the decoded bit stream is equal to the sent bit stream % bit_stream==decoded_bit_stream 11. 64 QAM transmitter clc clear all close all %generation of QAM %% %INITIALISATION f2=10e6; data_rate=1e6; no_of_cycles=f2/data_rate; fs=10*f2; ts=1/fs; t=0:ts:fs/f2*no_of_cycles*ts; %================================================

% GENERATION OF THE CARRIERS FOR THE BIT DURATION %================================================ sin_y=sin(2*pi*f2*t); cos_x=cos(2*pi*f2*t); sigstart=1; Tb=length(sin_y); sigend=Tb; mod_sig=0; %% for no_of_bits=1:1:1000 %% x_symb=[]; y_symb=[]; for x_random_no_stream =1:1:3 if rand > 0.5 bit='1'; x_symb=[x_symb bit]; else bit='0'; x_symb=[x_symb bit]; end % end of if loop %%

if rand > 0.5 bit='1'; y_symb=[y_symb bit]; else bit='0'; y_symb=[y_symb bit]; end % end of if loop end %end of 1st for loop for bit to symbol mapping %% x_amp=bin2dec(x_symb) y_amp=bin2dec(y_symb) signal=x_amp*cos_x-y_amp*sin_y; mod_sig(sigstart:sigend)=signal; sigstart=sigend+1; sigend=sigend+Tb; end %end of 2nd for loop for bit generation plot(mod_sig) fft_sig=fftshift(fft(mod_sig)); mag_fft_sig=abs(fft_sig); db_mag_fft_sig=10*log10(mag_fft_sig); faxis=linspace(-fs/2,fs/2,length(fft_sig)); maxi=max(db_mag_fft_sig); norm_db_mag_fft_sig=db_mag_fft_sig-maxi; figure plot(faxis,norm_db_mag_fft_sig)

save QAM_sig mod_sig cos_x sin_y 12. 64 QAM receiver clc clear all close all %psk receiver% load QAM_sig f=10e6; fs=10*f; data_rate=1e6; no_of_cycles=f/data_rate; ts=1/fs; t=0:ts:no_of_cycles*fs/f*ts; x=mod_sig; len=length(x); %================================================ % GENERATION OF THE CARRIERS FOR THE BIT DURATION %================================================ LO_I=cos(2*pi*f*t); LO_Q=sin(2*pi*f*t); len_LO=length(LO_I); cleaning_pt=0; % From observation of the received signal's plot x(1:cleaning_pt)=[]; len=len-cleaning_pt; remain=rem(len,len_LO) x(end-remain+1:end)=[]; x_mat=reshape(x,len_LO,[]); x_mat=x_mat'; I_mat=repmat(LO_I,size(x_mat,1),1); Q_mat=repmat(LO_Q,size(x_mat,1),1); prod_sigs=x_mat.*I_mat; int_prod_sigs_I=sum(prod_sigs,2); prod_sigs=x_mat.*Q_mat; int_prod_sigs_Q=sum(prod_sigs,2); plot(int_prod_sigs_I,int_prod_sigs_Q,'*') figure hist(int_prod_sigs_I) figure hist(int_prod_sigs_Q) % figure % stem(int_prod_sigs); % % % % % % % % % % int_prod_sigs(int_prod_sigs>0)=1; % int_prod_sigs(int_prod_sigs<0)=0; % int_prod_sigs=int2str(int_prod_sigs);% Convert to character for comparing with bit stream % decoded_bit_stream=int_prod_sigs'; % Just converting to column vector to comapare with bit_stream which is a column vector

% % Check whether the decoded bit stream is equal to the sent bit stream % bit_stream==decoded_bit_stream

13. DSBSC transmitter and Receiver


% Generate a dsbsc signal and store on hdd clc clear all close all fm=3e3;fc=10e3; fs=10*fc; ts=1/fs;no_of_cycles=100; t=0:ts:fs/fc*no_of_cycles*ts; m=sin(2*pi*fm*t); phi=0.8; c=cos(2*pi*fc*t+phi); rsc=m.*c; save rsc_file rsc % To generate the carrier with known frequency but unknown phase using a % squaring loop clear all; close all; clc load rsc_file plot(rsc); fc=10e3; fs=10*fc; f_digital=4*fc/fs; b = fir1(48,[f_digital-0.01*f_digital f_digital+0.01*f_digital]); figure db_plot_fft(b,1); % [mi,im]=min(abs(f-fc)) ; % phaseBPF=angle(IR(im)) ; q=rsc.^2; db_plot_fft(q,1); rp=filter(b,1,q); rp=rp./max(abs(rp)); rp(1:175)=[];% Just a random phase shift db_plot_fft(rp,fs) figure plot(rp) save rp_file rp

14. Pam4level
function [pulse_seq,bitstream]=pam_4level(sentence) % clc % clear all % close all % % pam % % Generate a message signal % sentence='This is iitguwahati'; dec_sentence=real(sentence); bin_sentence=dec2base(dec_sentence,2,8) bin_sentence=bin_sentence'; % The : operator works columnwise

bitstream=bin_sentence(:); bitstream=bitstream'; pulse_seq=[]; for ind=1:2:length(bitstream)-1 dibit=bitstream([ind,ind+1]); switch(dibit) case '00' pulseq=-3; case '01' pulseq=-1; case '10' pulseq=1; case '11' pulseq=3; end pulse_seq=[pulse_seq,pulseq]; end % plot(pulse_seq)

15. Baseband pulse transmission


clc clear all close all % Program to realize a DFT filter bank using filter command % Let us first try a baseband pulse shaping sentence='This is IIT Guwahati'; pulse_seq=pam_4level(sentence); header_seq=pam_4level('ABC'); pulse_seq=[header_seq,pulse_seq]; M=100; N=length(pulse_seq); pulse_seq_upconverted=zeros(1,M*N); pulse_seq_upconverted(1:M:end)=pulse_seq; figure,plot(pulse_seq_upconverted);title('pulse_seq_upconverted') pulse_shape=hamming(M); pulse_shape=pulse_shape'; pulse_modulated_signal=filter(pulse_shape,1,pulse_seq_upconverted); plot(pulse_modulated_signal); save pulsed_sig pulse_modulated_signal pulse_shape figure; plot(pulse_shape);title('Pulse shape');

Correlation
function [dp]=sumit_correlation(header,data); shift_header=1; len_header=length(header); len_data=length(data);

end_correlator=shift_header+len_header-1; while end_correlator<len_data; extracted_data=data(shift_header:end_correlator); dp(shift_header)=header*extracted_data'; shift_header=shift_header+1; end_correlator=shift_header+len_header-1; end

16. Baseband pulse receiver


%******************************************** %% Recevier %******************************************** recd_sig=pulse_modulated_signal; matched_filter=fliplr(pulse_shape); % It is not known at this point where the header starts. Hence the header pulse seq is % to be correlated to the directly received signal. (The matched filtered % signal is of no use until we know the sampling instances) %*** Find the location of header***************** % Generate a local header local_header=pam_4level('ABC'); % Convert this local header to a pulse seq local_header_upconverted=zeros(1,M*length(local_header)); local_header_upconverted(1:M:end)=local_header; pulse_shape=hamming(M); pulse_shape=pulse_shape'; local_header_pulse_seq=filter(pulse_shape,1,local_header_upconverted); % Correlate this pulse seq to the directly received signal correlated_sig=sumit_correlation(local_header_pulse_seq,recd_sig); [junk,ind]=max(correlated_sig) % Now we have the header location in ind figure;plot(correlated_sig);title('Correlated Signal for header location') % Extract the header from the signal recd_header_sig=recd_sig(ind:ind+length(local_header_pulse_seq)); recd_sig_wo_header=recd_sig(length(recd_header_sig)+1:end); % correlate signal with pulse_shape: A type of matched filtering matched_filtered_sig=sumit_correlation(matched_filter,recd_sig_wo_heade r); % Downsample the signal % this is equivalent to sampling the signal at proper instance recd_downsampled_sig=matched_filtered_sig(1:M:end); figure;stem(recd_downsampled_sig);title('Received and downsampled alphabets'); grid on %********************************************** %*********************************************** %*********************************************** %*****Derive a average gain coefficient using the header********

% len_header=length(recd_header_sig); % Extract the header seq from the received sig matched_filtered_recd_header_seq=sumit_correlation(pulse_shape,recd_hea der_sig); % Downconvert to extract the header seq recd_header_seq=matched_filtered_recd_header_seq(1:M:end); figure;stem(recd_header_seq);title('Received header sequence');grid on; hold on;stem(local_header,'r') % power_pulse_shape=sum((pulse_shape).^2)/length(pulse_shape); % recd_headerseq_norm=recd_headerseq/(power_pulse_shape*M); plot(a)

You might also like