# How can I create a for loop using simulated data for the start of frequency analysis?

8 views (last 30 days)
Raegan Windham on 5 May 2022
Answered: Mathieu NOE on 9 May 2022
I am in the middle of doing this code and have become confused. The instructions are below:
Use a for loop to create your signal. The signal should include random noise + stationary (amplitude=3) 60Hz electromagnetic noise + a number of source signals (number determined by numOfSources) each with a random frequency between 4 and 30 Hz, with amplitudes changing randomly through time. So for example your signal might look like noise + 60 Hz frequency (ampl 3) + 5 Hz freqs (random amplitude) + 12 Hz freqs (randomamplitude) + 25 Hz freqs (random amplitude) + ... In addition to the random noise and 60 Hz stationary interference, you will have as many source frequencies as determined by numofSources variable.
My work in the previous section contains declaring my variables which I will attach:
srate = 1000;
endTime = 5;
time = 0:1/srate:endTime;
numChans = 5;
numofSources = 10; % This is the number of my frequency sources.
signal = numChans*Length;
For the for loop section this is what I have so far and what I am asking for help on finishing:
for i=1:numChans
signal(i,:) = % To be adding random noise and a 60 Hz stationary signal
for k=1:numofSources % Add all the rest of my source signals
sfreq = 6
sampl = interp1 % Random Amplitude Vector
ssignal = % multiply the sampl with the regular sine waves for the specific freq
signal(i,:) = signal(i,:) + ssignal; % add the new source signal with the simulated signal
end
end

Mathieu NOE on 9 May 2022
hello
see code correction below ; there is only one for loop as we add the new signals on the 60 Hz + noise signal created before the for loop. Also, the random amplitudes and frequencies are created as an array , we simply pick the k th element in the for loop.
clc
clearvars
close all
srate = 1000;
endTime = 1;
time = 0:1/srate:endTime;
numofSources = 10; % This is the number of my frequency sources.
freq_rand_vect = randi([4 30],numofSources,1); % random frequency between 4 and 30 Hz;
ampl_rand_vect = rand(numofSources,1); % random amplitude between 0 and 1;
signal = 3*sin(2*pi*60*time) + 0.01*randn(size(time)); % noise + 60 Hz frequency (ampl 3)
figure(1), hold on
for k=1:numofSources % Add all the rest of my source signals
new_signal = ampl_rand_vect(k)*sin(2*pi*freq_rand_vect(k)*time); % new source signal
signal = signal + new_signal; % add the new source signal with the simulated signal
plot(time,signal)
pause(1)
end
hold off

R2021b

### Community Treasure Hunt

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

Start Hunting!