Paralell Execution of Functions

3 views (last 30 days)
Andrea Carroll
Andrea Carroll on 5 Feb 2020
Answered: Guillermo Soriano on 12 Dec 2023
%% Outline of this code:
%% - Reads excel file to gain the inputs; (column 1: time stamp values, column2: data value)
%% - when the timestamp reaches the same vale as the timer it is converted
%% into binary, the same is done to the flight number and then they are
%% concatenated and appended to the frame ready for transmission. This is
%% completed through the new function 'frame_generator' in the first if
%% statement.
%% - When the timer reaches an multiple of 10ms the frame is transmitted.
%% This is achieved through the new function 'transmission' in the 2nd if
%% statement.This functions includes:
%% - FSK modulation
%% - AWNG channel noise
%% - FSK demodulation (resulting in the original frame)
%% - Observations:
%% - In this version the whole transmission takes aorund 2 seconds to
%% execute:
%% - Start of transmission: 0.012207 seconds
%% - End of transmission: 0.020016 seconds 0.007809
%% - Excution time is much better and so in 100ms the first 76
%% impulses are processed, however 79 impulses exist (so in deficit
%% of 3)
%% - Changes:
%% - Two new functions: frame_generator, transmission
%% - Note:
%% - If multiple is 0.01, the 79 impluses cannot be reached
%% - If multiple if 0.02, then the 79 impluese are almost always reached
%%
%XXXXXXXXXXXXXXXXXXX This is the Main Function XXXXXXXXXXXXXXXXXXXXXXXXXX%%
function k = Frame_Transmission_v4_new()
clc;
%Reading table
t=readtable('Depart_SD.csv');
%Converting table to array type
A=table2array(t);
%Starts the timer%
tic;
%Variables
x_count=1;
index=1;
frame_final=[];
multiple=0.02;
while toc<0.11
%This section sifts through the excel file and converts the data
%into binary values at the correct points in time.
if A(x_count,1)/1000<=toc
disp('looping');
%Executes function which adds data to the overall frame
[index, frame_final] = frame_generator(x_count, A, index, frame_final);
%Updates variable
x_count=x_count+1
end
%This section will now focus on the transmission of the frame
%Frame transmission will occur every 10 ms
if toc>=multiple && A(x_count,1)/1000>multiple
disp('Start Transmitting frame');
toc
%This creates the entire 512 bit frame it if is not full already
frame_transmission= frame_padding(frame_final);
%Transmitts the frame, output is what is seen by the receiver
dataOut = transmission(frame_transmission);
disp('End Transmitting frame');
toc
%Updating variables
frame_final=[];
multiple=multiple+0.02;
end
end
end
%%
%XXXXXXXXXXXXXXXXXXXXXXXXX FRAME GENERATOR XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%
%XXXXXXXXXXXXXXXXXXXXXXXThis Function creates the frameXXXXXXXXXXXXXXXXXXX%
function [index, frame_final] = frame_generator(x_count, A, index, frame_final)
%Converts timestamp and flight no. into binary%
binary_data=binary_conversion(x_count,A);
%Addes the binary data to the overall frame%
[index, frame_final]=frame_creation(binary_data,index,frame_final);
end
%%
%XXXXXXXXXXXXXXXXXXXXXXXXX TRANSMISSION XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%
%XXXXXXXXXXXXXXXXThis Function transmitts the final frameXXXXXXXXXXXXXXXXX%
function dataOut = transmission(frame_transmission)
%Parameters
M = 2; % Modulation order
freqsep = 8; % Frequency separation (Hz)
nsamp = 8; % Number of samples per symbol
Fs = 32; % Sample rate (Hz)
%Apply FSK modulation.
FSK_mod = fskmod(frame_transmission,M,freqsep,nsamp,Fs);
%Apply AWGN
reqSNR=0.1;
AWGN_sig = awgn(FSK_mod,reqSNR);
%Apply FSK demodulation: final output
dataOut = fskdemod(AWGN_sig,M,freqsep,nsamp,Fs);
end
%%
%XXXXXXXXXXXXXXXXXXXXXXXXX BINARY CONVERSION XXXXXXXXXXXXXXXXXXXXXXXXXXXXX%
%XXThis Function converts the timestamp and flight no. into binary valuesX%
function final = binary_conversion(x_count, A)
x=de2bi(int64(A(x_count,1)), 24);
y=int64(A(x_count,2));
y_shift=bitshift(y,12);
d=de2bi(y_shift, 24);
final=x+d;
end
%%
%XXXXXXXXXXXXXXXXXXXXXXXXX FRAME CREATION XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%
%XXXXXXXXXXXThis functions adds the binary data to the frameXXXXXXXXXXXXXX%
function [index_no, frame_main] = frame_creation(data,index,frame_part)
frame_index=length(frame_part)+1;
for i=1:length(data)
%adds single bit to the frame
frame_part(frame_index)=data(i);
frame_index=frame_index+1;
end
index_no=frame_index;
frame_main=frame_part;
end
%%
%XXXXXXXXXXXXXXXXXXXXX Frame Padding XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%
% this function padds the frame will zeros to make 512 bit in total length%
function frame = frame_padding(frame_partial)
frame=frame_partial;
for i=length(frame_partial):1:511
frame(i+1)=0;
end
end
%%
Please note I will refer to function 'Frame Generator' as F1, and 'Transmission' as F2. What I want is for F1 to continuously run when there is an impulse (hypothetically), and for F2 to run every 10 seconds. The problem that I am having is that F1 can not whilst F2 is running. I show this problem in the picture below. I have tried with parfeval but no luck yet. It is important to note that F2 is dependent on F1, meaning that one of F2's input is the output of F1. Would anyone know how to solve this problem?

Answers (1)

Guillermo Soriano
Guillermo Soriano on 12 Dec 2023
I guess what are you trying to do is running twot therads at the same time with one sesion. As far as I know, is not possible, I have the same problem

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!