Modulace BPSK a QPSK

Modulace BPSK

%% BPSK
% Tomas Krejci, 11.4.2015

clc
close all
clear all

data = [1 1 0 1 0 0 0 1 1 1];
% = randi([0 1], n_data, 1)

%% BPSK

fs = 100E6;
Ts = 10E-6; % interval symbolu

n_data = length(data);
Tr = n_data * Ts; % delka celeho signalu
nn = fs*Ts; % pocet vzorku v symbolovem intervalu
t = 0 : 1/fs : Tr - 1/fs; 

y_c = zeros(1, nn * n_data);
y_s = zeros(1, nn * n_data);

for ii = 1 : n_data

 if data(ii) == 0
 y_c((ii-1)*nn+1 : ii*nn ) = 1;
 y_s((ii-1)*nn+1 : ii*nn ) = 1;
 else
 y_c((ii-1)*nn+1 : ii*nn ) = -1;
 y_s((ii-1)*nn+1 : ii*nn ) = -1;
 end

end
f = 1E6; % frekvence nosne
s = y_c .* cos(2*pi*f*t) + y_s .* sin(2*pi*f*t); % vysledny signal

S= 10*log10((abs(fftshift(fft(s)))).^2);% energetické spektrum signálu
fx = fs/2 * linspace(-1,1,length(s)); 

figure;
subplot(2,1,1); plot(t,s); title('BPSK'); xlabel('t [s]'); ylabel('A [V]');
subplot(2,1,2); plot(fx,S); title('Spektrum');xlabel('f [Hz]'); ylabel('Se [dB]');
 ylim([-20 max(S)*1.2 ]); xlim([-3E6 3E6]);

 

Modulace QPSK

%% QPSK
% Tomas Krejci, 11.4.2015

clc 
close all
clear all
 
data = [1 1 0 1 0 0 0 1 1 1];

fs = 100E6; 
Ts = 10E-6; % interval symbolu
Nb = 2; % Q=4 -> 4=2^2
n_data = length(data);
Tr = n_data/Nb * Ts; % delka celeho signalu
nn = fs*Ts; % pocet vzorku v symbolovem intervalu
t = 0 : 1/fs : Tr - 1/fs; 

y_c = zeros(1, nn * n_data/Nb);
y_s = zeros(1, nn * n_data/Nb);
jj = 1;
for ii = 1 : Nb : n_data-1 
 if data(ii)==0 && data(ii+1)==0
 y_c( (jj-1)*nn+1 : jj*nn ) = 0.5;
 y_s( (jj-1)*nn+1 : jj*nn ) = 0.5;
 end
 if data(ii)==0 && data(ii+1)==1
 y_c( (jj-1)*nn+1 : jj*nn ) = -0.5;
 y_s( (jj-1)*nn+1 : jj*nn ) = 0.5;
 end 
 if data(ii)==1 && data(ii+1)==0
 y_c( (jj-1)*nn+1 : jj*nn ) = -0.5;
 y_s( (jj-1)*nn+1 : jj*nn ) = -0.5;
 end 
 if data(ii)==1 && data(ii+1)==1
 y_c( (jj-1)*nn+1 : jj*nn ) = 0.5;
 y_s( (jj-1)*nn+1 : jj*nn ) = -0.5;
 end 
 jj = jj + 1;
end


f = 1E6; % f nosne
s = y_c .* cos(2*pi*f*t) + y_s .* sin(2*pi*f*t); % vysledny signal

S= 10*log10((abs(fftshift(fft(s)))).^2);% energetické spektrum signálu 
fx = fs/2 * linspace(-1,1,length(s)); 

figure;
subplot(2,1,1); plot(t,s); title('QPSK'); xlabel('t [s]'); ylabel('A [V]');
subplot(2,1,2); plot(fx,S); title('Spektrum');xlabel('f [Hz]'); ylabel('Se [dB]'); 
 ylim([-20 max(S)*1.2 ]); xlim([-3E6 3E6]);</pre>
<pre>

Download 

QPSK BPSK

Modulace ASK

Modulace ASK. Signály RC a RRC.
%% ASK
% Tomas Krejci, 11.4.2015

clc
close all
clear all

data = [1 1 0 1 0 0 0 1];

%% PAM - jedno rozmerna
fs = 1E9; % vzorkovaci frek, pro "udelani spojite s(t)", v prednskach jako fh=1/Tv
Ts = 1E-6; % symbolovy interval, (jak dlouho potrva jeden datovy syombol (1 nebo 0 z data) po modulaci jako s(t) ?)
nn = fs*Ts;
kMax = length(data); % kolik je symbolovych intervalu (pro jedno rozmernou odpovida datovych intervalu)

c_m = [0 1]; % dvou-stavová
M = length(c_m);
c_m_k = zeros(1, kMax);
for kk = 1:kMax

 switch (data(kk))
 case 0
 c_m_k(kk) = c_m(1);
 case 1
 c_m_k(kk) = c_m(2);
 otherwise
 c_m_k(kk) = 0;
 end
end

%% REC
h_xy = ones(1, nn);
%% BiP
h_xy = [ones(1, nn/2) -ones(1, nn/2)];
%% RC
trc = -1:1/(nn/2):1-1/(nn/2);
h_xy = sRC(0.5,trc,nn-1);
%% RRC
h_xy = sRRC(0.5,trc,nn-1);

%% PAM expanzni cast
s_PAM = zeros(1, nn * kMax);

for kk = 1:kMax
 s_PAM( (kk-1)*nn+1 : kk*nn ) = c_m_k(kk) * h_xy(1,:);
end

%% ASK
f0 = 100E6;
w0 = 2*pi*f0;
t0 = 0:1/fs:kMax*Ts-1/fs;
% ASK
s_t = s_PAM .* cos(w0*t0);
% spektrum
S_PAM = 10*log10((abs(fftshift(fft(s_t)))).^2);% energetické spektrum signálu
fx = fs/2 * linspace(-1,1,length(s_t)); 

h_figure = figure; set(h_figure(:),'color',[1 1 1]);
subplot(211); hold all;
 plot(t0, s_t, 'Color', 'red', 'LineWidth', 2);
 ylim([min(s_t)*1.2 max(s_t)*1.2 ]); title(['ASK M=' num2str(M)]);
 xlabel('Time [s]'); ylabel('A [V]'); %legend('PAM s(t)', 'c_m_(_k_)','Location','NorthEastOutside');
subplot(212); hold all;
 plot(fx, S_PAM, 'Color', 'blue');
 ylim([-40 max(S_PAM)*1.2 ]); title('Spektrum');
 xlabel('f [Hz]'); ylabel('Se [dB]');

 

Signál RC

function s_RC = sRC(alfa, t, N)
s_RC = zeros(1,N+1);
for n = 1:N+1,
 if abs(t(1,n))<= (1-alfa)/2,
 s_RC(1,n) = 1;
 end;
 if abs(t(1,n))<= (1+alfa)/2,
 if abs(t(1,n))>= (1-alfa)/2,
 s_RC(1,n)=(cos((pi/4/alfa)*(2*abs(t(1,n))-1+alfa)))^2;
 end;
 end;
end;

Signál RRC

function s_RRC = sRRC(alfa, t, N)
s_RRC = zeros(1,N+1); 
for n = 1:N+1,
 if t(1,n) ==0, s_RRC(1,n) = 1-alfa+4*alfa/pi;
 elseif abs(t(1,n))==1/4/alfa, s_RRC(1,n) = (alfa/pi/1.414)*((pi-2)*cos(pi/4/alfa)+(pi+2)*sin(pi/4/alfa));
 else s_RRC(1,n) = ((sin((1-alfa)*pi*t(1,n)))/pi/t(1,n)+(4*alfa*cos((1+alfa)*pi*t(1,n)))/pi)/(1-16*alfa*alfa*t(1,n)*t(1,n)); 
 end;
end;

Download 

ASK

sRC sRRC

Modulace PAM #2

Modulace PAM #2 - vícerozměrná
%% PAM #2
% Tomas Krejci, 11.4.2015

clc 
close all
clear all

data = [0 1 0 0 0 0 0 1 ... % A
 0 1 1 0 1 0 0 0 ... % h
 0 1 1 0 1 1 1 1 ... % o
 0 1 1 0 1 0 1 0]; %j


%% PAM - vice rozmerna

fs = 1E6; % vzorkovaci frek, pro "udelani spojite s(t)", v prednskach jako fh=1/Tv
Ts = 1E-3; % symbolovy interval, (jak dlouho potrva jeden datovy syombol (1 nebo 0 z data) po modulaci jako s(t) ?)
nn = fs*Ts;

N = 4; % kolik rozmer mod.
Nb = log2(N); % za prepokladu dat ve dvojkove soustave, pocet bitu predstavuji jednu modulacni fci h neboli stav q
kMax = length(data)/Nb; % kolik je symbolovych intervalu (pro jedno rozmernou odpovida datovych intervalu (poctu dat))
M = 2; % uvazujeme dvou svavovou modulaci 


%% REC
h_m = [ones(1, Nb*nn); 
 [ones(1, nn) -ones(1, nn)]; 
 [ones(1, nn/2) -ones(1, nn) ones(1, nn/2)];
 [ones(1, nn/2) -ones(1, nn/2) ones(1, nn/2) -ones(1, nn/2)] ];
q = zeros(1,kMax);
jj =1;
for ii = 1:Nb: length(data)-1
 
 if data(ii)==0 && data(ii+1)==0
 q(jj) = 1;
 end
 if data(ii)==0 && data(ii+1)==1
 q(jj) = 2;
 end 
 if data(ii)==1 && data(ii+1)==0
 q(jj) = 3;
 end 
 if data(ii)==1 && data(ii+1)==1
 q(jj) = 4;
 end 
 jj = jj+1;
 
end


%% PAM expanzni cast
s_t = zeros(1, nn * length(data));

for kk = 1:kMax
 s_t( (kk-1)*nn*Nb+1 : kk*nn*Nb ) = h_m(q(kk),:); 
end

t_osa = 0 : 1/fs : kMax*Ts*Nb-1/fs;
c_osa = 0 : Nb*nn/fs : kMax*Ts*Nb-1/fs;
q_osa = 0 : Nb*nn/fs : kMax*Ts*Nb-1/fs;
h_figure = figure; set(h_figure(:),'color',[1 1 1]);
subplot(211); hold all; 
 % [hAx,hLine1,hLine2] = plotyy(t_osa,s_t,c_osa,q,'plot','stem');
 plot(t_osa, s_t, 'Color', 'red', 'LineWidth', 2); 
 stem(c_osa, q/4, 'Color', 'blue', 'LineStyle', '--', 'Marker', '>', 'LineWidth', 2);
 % hLine2.LineStyle = '--'; hLine2.Marker = '>'; hLine2.LineWidth = 2;
 ylim([min(s_t)*1.2 max(s_t)*1.2 ]); title(['PAM M=' num2str(M)]);
 xlabel('Time [s]'); ylabel('A [V]'); legend('PAM s(t)', 'q_m_k','Location','NorthEastOutside');
subplot(212); hold all; 
 stem([0:1:Nb*kMax-1], data, 'Color', 'Black', 'LineStyle', '-', 'Marker', 'x', 'LineWidth', 2);
 ylim([min(data)*1.2 max(data)*1.2 ]); title('data');
 xlabel('Time [n]'); ylabel('[-]'); legend(' data', 'Location','NorthEastOutside');

Download 

PAM_2

Modulace PAM #1

Modulace PAM #1 - jednorozměrná
%% PAM #1
% Tomas Krejci, 11.4.2015

clc
close all
clear all

%data = [1 1 0 1 0 0 0 1];
data = [0 1 2 3 0 3 2 1];

%% PAM - jedno rozmerna
% modulační funkce h jsou nasobky modulačniho pulzu, např obdelnikove pulzy, h0 = 1V, h1=2V,...
fs = 1E6; % vzorkovaci frek, pro "udelani spojite s(t)", v prednskach jako fh=1/Tv
Ts = 1E-3; % symbolovy interval, (jak dlouho potrva jeden datovy syombol (1 nebo 0 z data) po modulaci jako s(t) ?)
nn = fs*Ts;
kMax = length(data); % kolik je symbolovych intervalu (pro jedno rozmernou odpovida datovych intervalu)

c_m = [-2 -1 1 2]; % nasobici koeficienty pro modulacni fci
M = length(c_m);
c_m_k = zeros(1, kMax);
for kk = 1:kMax

 switch (data(kk))
 case 0
 c_m_k(kk) = c_m(2);
 case 1
 c_m_k(kk) = c_m(3);
 case 2
 c_m_k(kk) = c_m(1);
 case 3
 c_m_k(kk) = c_m(4);
 otherwise
 c_m_k(kk) = 0;
 end
end

%% REC
h_xy = ones(1, nn);
%% BiP
%h_xy = [ones(1, nn/2) -ones(1, nn/2)];

%% PAM expanzni cast
s_t = zeros(1, nn * kMax);

for kk = 1:kMax
 s_t( (kk-1)*nn+1 : kk*nn ) = c_m_k(kk) * h_xy(1,:);
end

t_osa = 0 : 1/fs : kMax*Ts-1/fs;
c_osa = 0 : nn/fs : kMax*Ts-1/fs;

h_figure = figure; set(h_figure(:),'color',[1 1 1]);
subplot(211); hold all;
 plot(t_osa, s_t, 'Color', 'red', 'LineWidth', 2);
 stem(c_osa, c_m_k, 'Color', 'blue', 'LineStyle', '--', 'Marker', '>', 'LineWidth', 2);
 ylim([min(s_t)*1.2 max(s_t)*1.2 ]); title(['PAM M=' num2str(M)]);
 xlabel('Time [s]'); ylabel('A [V]'); legend('PAM s(t)', 'c_m_(_k_)','Location','NorthEastOutside');
subplot(212); hold all;
 stem([0:1:kMax-1], data, 'Color', 'Black', 'LineStyle', '-', 'Marker', 'x', 'LineWidth', 2);
 ylim([min(data)*1.2 max(data)*1.2 ]); title('data');
 xlabel('Time [n]'); ylabel('[-]'); legend(' data', 'Location','NorthEastOutside');

Download 

PAM_1

Konvoluce 2D / Fitrace obarzu

Numerický výpočet dvourozměrné konvoluce neboli filtrace obrazu.

 

</pre>
<pre>%% Konvoluce 2D / Fitrace obarzu
% Tomas Krejci, 11.4.2015

clc
close all
clear all
<!--more-->
%% konvolucni jadro / filtr
%h = [0 -1 0; -1 5 -1; 0 -1 0]; % doostreni
h = [1 1 1; 1 3 1; 1 1 1]; % rozostreni
h = h / sum(sum(h)); % normalizace

%% obraz
img = rgb2gray(double(imread('grey2.png'))/256);
%img = [1 1 1; .5 .5 .5; 0 0 0];

imgc = conv2(img,h); % pomoci normani konvoluce
imgc = imfilter(img,h); % pomoci funkce...
sp = abs(fftshift(fft2(h, 1024, 1024))); % spektrum 2D
%% rozsireni obrazu
% okrajove podminky.....
% predpoklad kernelu 3x3

[m n] = size(img);
img2 = zeros(m+2, n+2);
img3 = img2;
img2(2:end-1, 2:end-1) = img; % stred
img2(2:end-1, 1) = img(:, 2); % levy sloupec
img2(2:end-1, end) = img(:, end-1); % pravy sloupec
img2(1, 2:end-1) = img(2, :); % prvni radek
img2(end, 2:end-1) = img(end-1, :); % posledni radek
img2(1, 1) = img(2, 2); % levy horni roh
img2(1, end) = img(2, end-1); % pravy horni roh
img2(end, 1) = img(end-1, 2); % levy dolni roh
img2(end, end) = img(end-1, end-1); % pravy dolni roh

%% vypocet
[m n] = size(img2);
for mm = 2:m-1
for nn = 2:n-1
sub = img2( (mm-1):(mm+1), (nn-1):(nn+1) ) .* h; % pronasobeni danych boud/pixelu
img3(mm, nn) = sum( sum(sub) ); % soucet / vysledek 2D konvoluce
end
end
img4 = img3(2:end-1, 2:end-1); % orezani okraju

figure;
subplot(2, 2, 1); imshow(img); title('original');
subplot(2, 2, 2); mesh(sp); title('spketrum filtru');
subplot(2, 2, 3); imshow(imgc); title('po fitraci - imfilter');
subplot(2, 2, 4); imshow(img4); title('po fitraci - rucne');

Download

grey2

 

 

 

 

konvoluce_2D