Home > Mobile >  MATLAB code for adding two signals for different time intervals
MATLAB code for adding two signals for different time intervals

Time:06-05

I have got two sine waves shifted 180 degrees from each other. I would like to create another signal from these two, but for that I have to add separate intervals separately and the resultant should be continuous as well.

Here are two sine waves:

Two sine waves

t = 0:0.00001:0.02;

w= 2*pi*50;
ma = 0.8*sin(w*t);
mb = 0.8*sin(w*t-pi);

Now I want to create another signal mcm. For an interval "0 to 0.005 (quarter cycle)" I want mcm = 1 ma. For interval "0.005 to 0.01" I want mcm = 1 mb.

And likewise for the other two quarters.

How do we go about doing it?

CodePudding user response:

Edit after the question was changed in the comment below

Given the need to generalise this to multiple cycles (note the addition of the user defined input n = number of cycles), here is what I would suggest.

Firstly, each function has to be explicitly defined. Secondly, for each point along the length of t, the function that needs to be used at that data point needs to be (explicitly) defined.

Thus in the code below, functions and functionOrder are used to set the functions to be used, and the order/frequency in which they are to be used.

In the Calculate mcm section, a variable ind is used to iterate over the output, and select the function to be used at each point.

% User inputs
f = 50;
n = 3; % number of cycles
inc = 0.00001; % increment

% Preliminaries
t_interval = 1/f;
t = 0:inc:t_interval*n; 
t = t(1:end-1);

w = 2*pi*f;
ma = 0.8*sin(w*t);
mb = 0.8*sin(w*t-pi);

% Define mcm
f1 = @(ii) -1 - mb(ii);
f2 = @(ii)  1 - ma(ii);
f3 = @(ii) -1   mb(ii);
f4 = @(ii)  1 - mb(ii);
functions = {f1, f2, f3, f4};
functionOrder = [1 2 3 4];

% Calculate mcm
ind = repmat(functionOrder, round(t_interval/inc)/length(functionOrder), 1);
ind = reshape(ind, numel(ind), 1);
ind = repmat(ind, n, 1);

mcm = zeros(size(ind));
for ii = 1:length(ind)
    mcm(ii) = functions{ind(ii)}(ii);
end

% Plot
figure; plot(t,ma);
hold on; plot(t,mb);
plot(t, mcm);

================================================

Previous answer to the simpler question, without the need for generalisability

The way I would approach this would be a compromise between ease of use in the current situation and ease of replication/dynamism as the example changes.

First create a variable that is in indicator of where ma should be used and mb should be used. Note that using 0 and 1 makes future steps easier: if you had more functions to utilise in this piecewise operation, the construction of this indicator would have to be different.

ind = zeros(size(t)); %indicator
ind(t >= 0.000 && t < 0.005) = 1;
ind(t >= 0.010 && t <=0.015) = 1;

Then, the addition of ma and mb is relatively straightforward.

mcm = 1   ind.*ma   (1-ind).*mb;

[Note that this follows the mathematical formulation described in the text, but the resulting curve is not continuous. I do not see a way to do this so that ma and mb alternate every quarter cycle and the curve is also continuous.]

  • Related