Injecting (and combining) a sine wave after a period of time
Show older comments
Hello everyone,
So,I have this one signal of 100s seconds long. It is an EEG signal which looks very much like a random signal. I want to inject a sine wave tACS data (5s long sine wave) into it at 30s. But when I say injection, I didn't mean putting sine signal over another EEG signal. At the time 30-35 seconds, I wanted to combine the two signals via addition. Lastly, displaying the full 100s EEG signal with 30-35s: sine wave data (tACS)+EEG. I have attached few image to pictorially what I meant. Here is the current code, that I am working on:
load('Test_Sham_Alpha.mat');%load EEG data file
%plot(EEG.Time(1,1:46600)' , EEG.Data(1,1:46600)')
%define sine wave
%%Time specifications:
Fs = 500; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 5; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
Fc = 5; % hertz
A= 0.001;
x = A*sin(2*pi*Fc*t);
% Plot the signal versus time:
%figure;
%plot(t,x);%length t elements = 2500
%xlabel('time (in seconds)');
%title('tACS Signal');
%inject f 5 Hz tACS at time: 30 seconds of EEG data (Y= 30453934)
%for 5 seconds (25 cycles)
time= EEG.Time(1,1:46600)
for EEG.Time(1,15000:17500)%length of EEG.time =2501
%add y-axis value for both signals
y1= EEG.Data(1,1:46600);
y2= x
y= y1+y2
end
%i am not sure if the above make sense or now
%not sure how to continue
%for the figure: I wanted that the plot shows EEG of its full length data
%where between 30-35s of EEG, there is a signal combination (adding of tACS sine
%wave with the EEG)and the rest of EEG signal remained as it is
figure ('Name','Combined EEG+ tACS');
plot(t,y);%length t elements = 2500
xlabel('time (in seconds)');
I have also attached the EEG data that I extracted into an excel file (but only from 25s -40s because 100s is too big but it can easily be plot onto Matlab (fast too), but it is data that requires permission before posting it on public), if you have an idea on how to do it. There are other things that I want to add it on data: ramp-up features, and implementing envelope onto the signals etc. But before I go on to those things, I'd like to settle the basic issues. Note: I can do this on Excel but it couldn't handle that too much data, so I wanted to use maltab but I am not very good at the coding part. I am pretty sure it should be simple. I thought of cutting the datas and then concatenate them but I don't think that is efficient.
Please asisst me. I reallyyyy need help on this. I feel losst :'(
Regards,
Anis
Answers (1)
Rohit Pappu
on 1 Apr 2021
A plausible solution is as follows
- Generate the required sinusoidal signal
%define sine wave
%%Time specifications:
Fs = 500; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 5; % seconds
t = (0:dt:StopTime-dt); % seconds , assuming t is a 1xn vector to match the dimensions of EEG data
%%Sine wave:
Fc = 5; % hertz
A= 0.001;
x = A*sin(2*pi*Fc*t);
- Select the EEG data corresponding to the time stamps required (In this case, index 15001 to 17500)
y_to_be_modified = EEG.Data(1,15001:17500);
- Add the sine wave to the EEG data
y_injected = x+y_to_be_modified;
- Replace the original EEG data section with this injected data
EEG.Data(1,15001:17500) = y_injected;
- Plot the EEG Data
plot(EEG.Time(1,1:46600) , EEG.Data(1,1:46600))
18 Comments
Anis Fatini Abdul Aziz
on 8 Apr 2021
Rohit Pappu
on 9 Apr 2021
There are a few issues with the above code
- Line 18 :Only a sample of the signal is being plotted, not the entire signal. To plot the entire signal
plot(EEG.Time(1:46600), EEG.Data(1:46600));
- Line 14: Only the injected part is converted to nV, not the entire EEG Data. This can lead to skewed graph like this

An alternative would be as follows
%% Scale the entire EEG.Data to the same units as x
EEG.Data = scalingFactor*EEG.Data;
y_to_be_modified = EEG.Data(1,15001:17500);
y_injected = x+y_to_be_modified; % Assuming x and y_to_be_modified have the same units ie. nV
EEG.Data(1,15001:17500) = y_injected;
figure(3);
plot(EEG.Time(1,1:46600) , EEG.Data(1,1:46600)) % Plots the entire signal
Anis Fatini Abdul Aziz
on 13 Apr 2021
Edited: Anis Fatini Abdul Aziz
on 13 Apr 2021
Rohit Pappu
on 13 Apr 2021
In above case:
[y_upper, y_lower] = envelope(y_injected) % Generate the envelope for injected values
plot(EEG.Time(1,1:46600) , EEG.Data(1,1:46600)) % Plot the entire signal
hold on
plot(EEG.Time(1,15001:45000), y_upper, y_lower) % Overlay the envelope on the previous plot
hold off
Anis Fatini Abdul Aziz
on 14 Apr 2021
Rohit Pappu
on 14 Apr 2021
For plotting multiple signals on the same set of axes, please use hold. To customise the line colors , refer to this documentation.
A brief outline would be as follows:
[env_upper,env_lower] = envelope(EEG.Data(1,1:46600))
plot(EEG.Time(1,1:46600) , EEG.Data(1,1:46600)) % Plot the entire signal
hold on
plot(EEG.Time(1,1:46600), env_upper, EEG.Time(1,1:46600), env_lower) % Overlay the envelope on the above plot. Use line spec to change the line colors
hold off
[y_upper, y_lower] = envelope(y_injected) % Generate the envelope for injected values
hold on
plot(EEG.Time(1,15001:45000), y_upper, EEG.Time(1,15001:45000), y_lower) % Overlay the envelope of injected
% signal on the previous plot . Use Line spec to change the color of the line
hold off
Additional documentaion can be found here
Anis Fatini Abdul Aziz
on 14 Apr 2021
Anis Fatini Abdul Aziz
on 14 Apr 2021
Rohit Pappu
on 15 Apr 2021
Please refer to this documentation - Envelope extraction
Anis Fatini Abdul Aziz
on 15 Apr 2021
Rohit Pappu
on 16 Apr 2021
Could you please explain, what does making a certain signal follow another signal's envelope mean?
Thank you
Anis Fatini Abdul Aziz
on 16 Apr 2021
Rohit Pappu
on 16 Apr 2021
Based on the above problem statement, a possible solution is as follows
- B_extract = B(30:90)
- Modulate Signal B with A ie. B_extract+A.*B_extract (Amplitude modulation)
Anis Fatini Abdul Aziz
on 16 Apr 2021
Rohit Pappu
on 16 Apr 2021
Yes, they all need to be in the same unit
Anis Fatini Abdul Aziz
on 16 Apr 2021
Edited: Anis Fatini Abdul Aziz
on 17 Apr 2021
Anis Fatini Abdul Aziz
on 17 Apr 2021
Anis Fatini Abdul Aziz
on 21 Apr 2021
Categories
Find more on Biomedical Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!