The easiest way to generate a sine or cosine signal in MATLAB. With this library there are almost infinite ways to describe and generate a sinusoid signals.
To use ssin or scos, you need to download and add to your path Simple Input Parser which is a package that allows you to create functions with a more convenient interface for the users.
- Download the latest release of Simple Input Parser.
- Run the
install.m
script or theinstall
command inside the downloadedsimple-input-parser
folder. - Done. Now you have Simple Input Parser on your system.
- Download the latest release.
- Run the
install.m
script or theinstall
command inside the downloadedsmart-sinusoids
folder. - Done. You can use Smart Sinusoids (and of course Simple Input Parser too).
This installation method is powered by the MATLAB Library System.
- fast and easy signal generation
- flexible parameter handling
- arbitrary parameter configurations
- optional time vector generation
Symbol | Unit | Name |
---|---|---|
A | [] | signal amplitude |
phi | [°] | phase in degrees |
f | [Hz] | signal frequency |
fs | [Hz] | sample rate |
T | [s] | signal period |
dt | [s] | sample time |
N | [] | number of periods in the signal |
n | [] | sample count |
L | [s] | signal length |
x | [st] | optional time vector scaling (see details later) |
With these parameters there are 5 main generation methods for sinusoid signals. Each of them have alternatives that doesn't count as an individual generation method due to the used parameters can be derived from the others if you apply the following formulas: fs = 1/dt, T = 1/f and L=n*dt.
Method index | Required parameters | CT DT lock | Description |
---|---|---|---|
1 | n , N |
No | a signal consisting of n data points with N periods in it |
2 | L , N , fs |
Yes | L seconds long signal consisting N periodswith the frequency f |
3 | f , N , fs |
Yes | a signal sampled at fs sampling rate with N periods in it with the frequency f |
4 | f , n , fs |
Yes | Generating a sinusoid signal consisting of n data points sampled at fs sampling rate with the frequency f |
5 | f , L , fs |
Yes | a signal sampled at fs sampling rate with the duration of L seconds with the frequency f |
n
- number of samplesN
- periods in it
stem( ssin('n N', 200, 1.5) )
L
- signal lenghtN
- periods in the signalfs
- with a given sample frequency
stem( ssin('L N fs', 0.001, 3, 48e3) )
f
- signal frequencyN
- periods in the signalfs
- with a given sample frequency
stem( ssin('f N fs', 440, 1.3, 48e3) )
f
- signal frequencyn
- number of samplesfs
- with a given sample frequency
stem( ssin('f n fs', 440, 200, 48e3) )
f
- signal frequencyL
- signal lenghtfs
- with a given sample frequency
stem( ssin('f L fs', 800, 0.001, 48e3) )
In every case every parameter can be substituted with the eqvivalent
counterparst. ie. fT, fsdt, L~n,dt ...
The amplitude (A) and phase (phi) are optional with the defualt values A=1 and phi=0.
There are three output parameter configurations
In this case Smart Sinusouds will output the signal vector in place. This could be useful during plotting
stem( ssin('f N fs', 440, 1.3, 48e3) )
With this configuration you can save the signal vector into a variable.
s = ssin('f N fs', 440, 1.3, 48e3);
stem(s)
If you would like to use the time vector as well, you can generate it too. The possibble values are the following:
Time vector type | x parameter | Description |
---|---|---|
Sample count | index | Sample indexes from 1 to the number of samples. |
Normalized | norm | Normalized vector spans from 0 to 1. |
Time [s] | time or s | Time duration of the signal in seconds. |
Time [ms] | militime or ms | Time duration of the signal in seconds. |
[t,s] = ssin('f N fs x', 440, 1.3, 48e3, 'ms');
stem(t,s)
This project is under the MIT license. See the included license file for further details.