Simulation Prelab 2

Transcription

Simulation Prelab 2
Simulation Prelab 2
1. Generate Random Sequence with MATLAB
About the rcosdesign function. Since rcosdesign function and comm toolbox is not included in MATLAB
edition lower than 2013a, we may have to use other function instead. My MATLAB is 2012b & 2013b,
and I use rcosflt function instead. I suggest everyone to update to MATLAB 2013b.
We can generate 128 bits sequence, repeat by 8 times, see the FFT spectrum and do the Raised Cosine
Pulse Shaping by the following code. The following code is for Problem 1(a)(c) and Problem 3.
clc;clear all;close all;
%% Generate random sequence
n = 128;
w =2*pi/n:(2*pi)/n:2*pi;
x = randi([0 1],n,1); % Random binary data stream
%% FFT
y = fft(x,n);
ySpectrum = y.*conj(y);
subplot(1,3,1);
plot(w,db(ySpectrum));xlim([0 pi]);ylim([-20 100]); axis on;% Show the
Spectrum without DC
title('PRBS spectrum without repeating 8 times');
%% Expand the sequence by 8
xExpand = kron(x,ones(8,1));
lxExpand = length(xExpand);
yExpand = fft(xExpand);
we = 2*pi/lxExpand:2*pi/lxExpand:2*pi;
yESpectrum = yExpand.*conj(yExpand);
subplot(1,3,2);
plot(we,db(yESpectrum));xlim([0 pi]);ylim([-20 100]);axis on;
title('PRBS spectrum with repeating 8 times');
%% Square-root Raised Cosine Pulse shaping
yrcosine = rcosflt(x,1,8,'fir/sqrt',0.25); % Filter the data.
lyrcos = length(yrcosine);
wr = 2*pi/lyrcos:2*pi/lyrcos:2*pi;
yrcos = fft(yrcosine);
yrcosS = yrcos.*conj(yrcos);
subplot(1,3,3);
plot(wr, db(yrcosS));xlim([0 pi]);ylim([-20 100]);axis on;
title('PRBS spectrum with Square-Root Raised Cosine Filter');
The figures are shown below:
PRBS spectrum without repeating 8 times
PRBS spectrum with repeating 8 times
PRBS spectrum with Square-Root Raised Cosine Filter
100
100
100
80
80
80
60
60
60
40
40
40
20
20
20
0
0
0
-20
0
0.5
1
1.5
2
2.5
3
-20
0
0.5
1
1.5
Here is the figure in the slide:
And the double-sided Power Spectrum is shown here:
2
2.5
3
-20
0
0.5
1
1.5
2
2.5
3
We can see that without repeating, the spectrum is noise-like; with repeating, the spectrum is sinc-like.
With Raised Cosine Pulse Shaping, the high frequency components are suppressed, which means Raised
Cosine Filter can limit the bandwidth of the signal we want to transmit.
Since the FFT point is limited, we cannot get full resolution of Power Spectrum. Also we didn’t use any
window for dealing with the spectrum, that’s why the spectrum doesn’t looks smooth.
2. Generate Random Sequence with Simulink and show the spectrum
The following part is for Problem 1(b)(c) and Problem 4.
To generate PN sequence, we use PN sequence generator. You can change the length of the sequence
by check the Frame-based Output, and set the number to be 127.
We can use Spectrum Scope to see the spectrum of the sequence. I set the spectrum to be one-sided, so
that we don’t need to see the negative frequency component. (Since the signal is real, its spectrum is
symmetric, we just need to see half.) Also you should set the y axis’s max and min limit. In the
attachment file, I set that to be y max to be 20, while the min is -50. You can change the parameter as
needed.
For repeating the sequence, it’s nasty to draw in Simulink. A better method is use MATLAB function. You
can check the attachment file for help.
The output of the three spectrum scope is shown below:
FFT Spectrum of Random Binary bits without repeating
FFT Spectrum of Random Binary bits with repeating 8 times
Magnitude Response (dB)
10
0
Magnitude (dB)
-10
-20
-30
-40
-50
-60
0
0.1
0.2
0.3
0.4
0.5
0.6
Normalized Frequency ( rad/sample)
0.7
0.8
The Frequency Response of Square-Root Raised Cosine Filter
0.9
FFT Spectrum of Random Binary bits with Square-Root Raised Cosine Filter
Compared to the picture of slide 1B, we can see that with Square-Root Raised Cosine Filter, the signal’s
high frequency component is suppressed, which means with Raised Cosine Pulse Shaping, the signal can
be band-limited.
3. Mitra, 13-1 Upsampling
This part is for Problem 2.
We can do up-samping by the following code:
clc;clear all;close all;
%% Generate the input of two sinusoidal sequence
N = 50; % Input length
n = 0:N-1;
x = sin(0.20*n)+sin(0.45*n);% x is sum of two sinusoidal sequence.
y = n;% y is ramp sequence
%% Generate the up-sampling of factor of 4.
xup = zeros(1, 4*length(x));
xup([1: 4: 4*length(x)]) = x;
yup = zeros(1, 4*length(y));
yup([1: 4: 4*length(y)]) = y;
%% Plot the input and the output sequences
figure;
subplot(2,1,1)
stem(n,x);
title('Input Sequence of Sum of two sinusoidal sequence');
xlabel('Time index n');ylabel('Amplitude');
subplot(2,1,2)
stem(xup);
title(['Output sequence up-sampled by 4']);
xlabel('Time index n');ylabel('Amplitude');
figure;
subplot(2,1,1)
stem(n,y);
title('Input Sequence of Ramp Sequence');
xlabel('Time index n');ylabel('Amplitude');
subplot(2,1,2)
stem(yup);
title(['Output sequence up-sampled by 4']);
xlabel('Time index n');ylabel('Amplitude');
The graph is shown down here:
Input Sequence of Sum of two sinusoidal sequence
2
Amplitude
1
0
-1
-2
0
5
10
15
20
25
Time index n
30
35
40
45
50
120
140
160
180
200
30
35
40
45
50
120
140
160
180
200
Output sequence up-sampled by 4
2
Amplitude
1
0
-1
-2
0
20
40
60
80
100
Time index n
Input Sequence of Ramp Sequence
50
Amplitude
40
30
20
10
0
0
5
10
15
20
25
Time index n
Output sequence up-sampled by 4
50
Amplitude
40
30
20
10
0
0
20
40
60
80
100
Time index n
Change all 4 in the code to be 7. We get the following graph:
Input Sequence of Sum of two sinusoidal sequence
2
Amplitude
1
0
-1
-2
0
5
10
15
20
25
Time index n
30
35
40
45
50
Output sequence up-sampled by 4
2
Amplitude
1
0
-1
-2
0
50
100
150
200
250
300
350
Time index n
Input Sequence of Ramp Sequence
50
Amplitude
40
30
20
10
0
0
5
10
15
20
25
Time index n
30
35
40
45
50
Output sequence up-sampled by 4
50
Amplitude
40
30
20
10
0
0
50
100
150
200
250
Time index n
4. Gaussian, Rayleigh & Rician Distribution by MATLAB
This part is for Problem 5. We finish problem 5 with following code:
clc;clear all;close all;
%% Generate noise vectors
sigma = 0.1;
N = 10000;
Ni = normrnd(0,sigma, [1 N]);
Nq = normrnd(0,sigma, [1 N]);
%% histogram with histfit function of two independent vectors
figure;
subplot(1,2,1);
histfit(Ni);title('Ni HistFit');
title('Histogram for Noise In-phase Vector');
300
350
subplot(1,2,2);
histfit(Nq);title('Nq HistFit');
title('Histogram for Noise Quadrature Vector');
%% histogram with histfit for magnitude of the noise
figure;
MagN=sqrt(Ni.^2+Nq.^2);
histfit(MagN);
title('Histogram for Noise Magnitude');
%% histogram with histfit for phase of the noise
figure;
PhaseN = atan(Nq./Ni);
histfit(PhaseN);
title('Histogram for Noise Phase');
%% Calculte the sample mean
mean(abs(Ni))
mean(abs(Nq))
mean(MagN)
% Add constant signal for I component
Ni01 = Ni + 0.1;
Ni05 = Ni + 0.5;
Ni10 = Ni + 1.0;
N01 = sqrt(Ni01.^2+Nq.^2);
N05 = sqrt(Ni05.^2+Nq.^2);
N10 = sqrt(Ni10.^2+Nq.^2);
figure;
subplot(1,3,1);
histfit(N01);xlim([0 1.5]);
title('Add 0.1 to I''s Histogram ');
subplot(1,3,2);
histfit(N05);xlim([0 1.5]);
title('Add 0.5 to I''s Histogram ');
subplot(1,3,3);
histfit(N10);xlim([0 1.5]);
title('Add 1.0 to I''s Histogram ');
figure;
subplot(1,3,1);
PhaseN01 = atan(Nq./N01);
histfit(PhaseN01);
title('Phase, Add 0.1 to I''s Histogram ');
subplot(1,3,2);
PhaseN05 = atan(Nq./N05);
histfit(PhaseN05);
title('Phase, Add 0.5 to I''s Histogram ');
subplot(1,3,3);
PhaseN10 = atan(Nq./N10);
histfit(PhaseN10);
title('Phase, Add 1.0 to I''s Histogram ');
The histogram of separate In-phase and quadrature noise component is shown below:
Histogram for Noise In-phase Vector
Histogram for Noise Quadrature Vector
350
350
300
300
250
250
200
200
150
150
100
100
50
50
0
-0.4
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
0
-0.4
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
We can see that independent I and Q noise is Gaussian-like.
The histogram of magnitude of the noise is shown below:
Histogram for Noise Magnitude
300
250
200
150
100
50
0
-0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
The magnitude of noise is obviously not Gaussian. The shape is Rayleigh distribution. Since
when X, Y is Gaussian(N(0,σ^2)), Z is Rayleigh(σ). As the note mentioned, the mean of
Rayleigh distribution is
. The mean of noise magnitude is 0.1264, sigma is 0.1 as we set,
sqrt(pi/2)*0.1 is 0.1253. So the expectation of Rayleigh(0.1), 0.1253, is approximately the mean of the
noise magnitude.
The phase distribution is shown below:
Histogram for Noise Phase
140
120
100
80
60
40
20
0
-3
-2
-1
We can see that the phase is uniform.
For part(b), The histograms are shown below:
0
1
2
3
Add 0.1 to I's Histogram
Add 0.5 to I's Histogram
300
Add 1.0 to I's Histogram
350
350
300
300
250
250
200
200
150
150
100
100
50
50
250
200
150
100
50
0
0
0.5
1
1.5
0
0
0.5
1
1.5
0
0
0.5
1
1.5
We can see that the distribution is Rice Distribution.
The phase is shown below:
Phase, Add 0.1 to I's Histogram
Phase, Add 0.5 to I's Histogram
Phase, Add 1.0 to I's Histogram
600
300
300
500
250
250
400
200
200
300
150
150
200
100
100
100
50
50
0
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
0
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
0
-0.4
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
As the constant on I is small, the distribution still looks like Rayleigh distribution. However, as the
constant increases, the phase distribution become thinner. The phase distribution is Gaussian-like,
which consists with what the note shows.