Hans
Hans

Reputation: 27

How can I create n sine waves from the elements of an n-by-m matrix?

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

Upvotes: 1

Views: 1353

Answers (1)

Trogdor
Trogdor

Reputation: 1346

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.

Upvotes: 1

Related Questions