forked from kaliliofs/Optisystem-and-MATLAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatlabOptiSysBER.m
97 lines (71 loc) · 3.69 KB
/
MatlabOptiSysBER.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
clear all;
NrOfInt = 255; % Number of integers per frame
M = 8; % Number of bits per symbol
NrFrames = 10; % Number of frames to be transmitted
% Generate the matrix with integers to be transmitted
IntSignalin = randint(NrOfInt,NrFrames,2^M-1);
% Converte integers to binary numbers
for swint = 1:NrFrames
BinarySig(swint,:) = reshape( dec2bin(IntSignalin(:,swint)) , NrOfInt * M, 1 );
end
% Setup bit rate and time window for tranmission of each frame
BitRate = 2.48832e9; % bits/s
NrOfBits = NrOfInt*M + 2;
TimeWindow = NrOfBits / BitRate;
% Calculate global parameters of OptiSystem to receive the bit sequence
% from Matlab
GlobalNrOfBits = 1024;
GlobalBitRate = GlobalNrOfBits / TimeWindow;
% OptiSystem cosimulation -------------------------------------------------
% create a COM server running OptiSystem
optsys = actxserver('optisystem.application');
% This pause is necessary to allow OptiSystem to be opened before
% call open the project
pause(15);
% Open the OptiSystem file defined by the path
optsys.Open('C:\Program Files\Optiwave Software\OptiSystem 10\samples\Matlab cosimulation/OpticalLinkProject.osd');
% Specify and define the parameters that will be varied
ParameterName1 = 'Filename';
InputSignal = 'OptiSysSequence.dat';
Document = optsys.GetActiveDocument;
LayoutMngr = Document.GetLayoutMgr;
CurrentLyt = LayoutMngr.GetCurrentLayout;
CurrentSweep = CurrentLyt.GetIteration;
Canvas = CurrentLyt.GetCurrentCanvas;
% Set the global parameters correctly
CurrentLyt.SetParameterValue('Bit rate', GlobalBitRate);
CurrentLyt.SetParameterValue('Simulation window', 'Set time window');
CurrentLyt.SetParameterValue('Time window', TimeWindow);
% Specify the components that will have the parameters updated
Component1 = Canvas.GetComponentByName('User Defined Bit Sequence Generator');
Component1.SetParameterValue( 'Bit rate', BitRate/1e9 )
Component2 = Canvas.GetComponentByName('Low Pass Bessel Filter');
Component2.SetParameterValue('Cutoff frequency', 0.75 * BitRate/1e9);
Component3 = Canvas.GetComponentByName('Data Recovery');
Component3.SetParameterValue('Reference bit rate', BitRate/1e9);
Visualizer1 = Canvas.GetComponentByName('Binary Sequence Visualizer');
for swint = 1 : NrFrames
OptiSysSequence(:,1) = str2num(BinarySig(swint,:)');
save OptiSysSequence.dat OptiSysSequence -ascii;
% vary the parameters, run OptiSystem project and get the results
% Set component parameters
Component1.SetParameterValue( ParameterName1, InputSignal );
% Calculate
Document.CalculateProject( false , true);
% get the bit sequence recovered from OptiSystem
GraphBinary = Visualizer1.GetGraph('Amplitude');
nSize = GraphBinary.GetNrOfPoints;
arrSig = GraphBinary.GetYData( CurrentSweep );
SignalOut = cell2mat( arrSig );
BinaryOut(:,swint) = SignalOut(3:2:2*(NrOfBits-1),1);
BinaryIn(:,swint) = OptiSysSequence(:,1);
BinaryMat = reshape(BinaryOut(:,swint), M, NrOfInt )';
% convertes the binary data to integer
Intmsg = bin2dec(num2str(BinaryMat));
end
% Calculates the bit error rate and the number of errors detected
[number,ratio] = symerr(BinaryOut,BinaryIn);
disp('The BER calculated is:')
disp(num2str(ratio))
% close OptiSystem
optsys.Quit;