1

I'm writing a program on MATLAB that generates 13 waveforms of varying amplitude, duration, and frequency. Each waveform is repeated 5 times, which means I have 65 'trials' in total.

The total length of each trial = 1.5 ms. The sampling frequency = 4 kHz. I would like the wave to begin at 0.5 ms. Prior to the onset of the wave, and following its offset, I would like the amplitude to be zero (i.e. a 'flatline' prior to and following the wave).

I have created a 65x3 matrix where the columns denote the frequency ('hz'), amplitude ('a'), and duration (ms) of the 65 sine waves. Each row denotes a single wave.

I would like to use the information contained in this 65x3 matrix to generate 65 sine waves of amplitude 'a', frequency 'hz', and duration 'ms'. To be specific: each wave should be created using the parameters (hz,a,ms) specified in the nth row of the matrix. E.g. if row 1 = 100, 1, 50... this means I would like to generate a 100 Hz sine wave (amplitude = 1) lasting 50 ms.

I have attempted to construct a for loop to solve this problem. However, the loop returns a number of errors, and I'm not sure how to resolve them. I have adapted the code to the point where no errors are returned; however, my latest attempt seems to generate 65 waves of equal duration, when in fact the duration of each wave should be that which is stated in vector 'ms'.

Here is my latest, albeit newbie and still unsuccessful, attempt: (note that 'trials' represents the 65x3 matrix discussed above; mA = amplitude).

hz=trials(:,1); mA=trials(:,2); ms=trials(:,3); 
trials_waves=zeros(65,500); % the max duration (= 500ms); unsure of this part?
for n = 1:size(order,1)
    trials_waves = mA*sin(2*pi*hz*0:ms);
end

Apologies if the information provided is scarce. This is the first time I have asked a question on this website. I can provide more information if needed.

Thank you for your help.

Best, H

Hans
  • 27
  • 1
  • 7

1 Answers1

1

Looks like you've got a good start, I'll try to help you get further towards your solution.

Make a Sine Wave

For starters, let's make a sine wave with variable rate, amplitude, and length.

Fs = 4e3; % sample rate of 4 kHz
Sr = 100; % example rate
Sa = 1;   % amplitude
St = 10e-3; % signal duration is 10 ms

% To create a sine wave in MATLAB, I'm going to first create a vector of time, 
% `t`, and then create the vector of sine wave samples.

N = St * Fs; % number of samples = duration times sample rate;
t = (1:N) * 1/Fs; % time increment is one over sample rate

% Now I can build my sine wave:

Wave = Sa * sin( 2 * pi * Sr * t );
figure; plot(t, Wave); 

Note! This is barely enough time for a full wavelength, so be careful with slow rates and short time lengths.

Make many Sine Waves

To turn this into a loop, I need to index into vectors of input variables. Using my previous example:

Fs = 4e3; % sample rate of 4 kHz
Sr = [100 200 300]; % rates
Sa = [1 .8 .5];   % amplitudes
St = [10e-3 20e-3 25e-3]; % signal durations

nWaves = length(Sr);

N = max(St) * Fs; % number of samples = duration times sample rate;
t = (1:N) /Fs; % time increment is one over sample rate
% initialize the array
waves = zeros(nWaves, N);

for iWaves = 1:nWaves
    % index into each variable
    thisT = (1:St(iWaves) * Fs) * 1/Fs;
    myWave = Sa(iWaves) * sin( 2 * pi * Sr(iWaves) * thisT );
    waves(iWaves,1:length(myWave)) = myWave;
end
figure; plot(t, waves); 

You still have one more piece, zero padding the front end of your signals, there's lots of ways to do it, one way would be to build the signal the way I've described and then concatenate an appropriate number of zeros to the front of your signal array. Feel free to ask a new question if you get stuck. Good luck!

Trogdor
  • 1,346
  • 9
  • 16
  • Hi Trogdor. I couldn't find an option to send you a personal message, but I just want to say thank you for your answer. You have helped me immensely and I really appreciate it. I adapted the code slightly and the problem has now been resolved. Thanks again for your help! -H – Hans Dec 13 '16 at 16:35
  • @Hans, you're very welcome, If you upvote my answer and mark it as accepted that's all the thanks I need :) – Trogdor Dec 13 '16 at 16:59