You are on page 1of 5

Signals in MatLab

How to describe a sinusoid in MatLab


An analog sinusoid can be described by the function x(t) = A.sin(2.f.t + )
A is the amplitude, f the frequency and is the phase angle in radians.
Assume that we will generate a sinusoid with N samples that includes f periods.
Calculate a set of number to be used as a time scale.
N = 0:N-1; % The numbers are n(1) = 0, N(2) = 1,.. n(N) = N-1
The frequency f can be interpreted as the number of periods per sample (f/N), since we
obtain f periods in N samples.
The signal can be described as: x = sin(2**f*n/N + );
During the total sampling period T = 1 sec we will get f periods and N samples. The sampling
time ts = 1/N sec. The sampling frequency fs = 1/ts = N Hz.
The Fourier transform is: Fx = fft(x);
Magnitude spectrum is: MFx = abs(fft(x)); or
Plot the spectrum : plot(MFx) or stem(MFx)

MFx = abs(Fx);

Frequency analysis of an even number of samples give the Nyqvist frequency at N/2 Hz and
the frequency resolution is df = 1/T = 1 Hz.
On the horizontal axis, the positive frequencies occupy the left half of the axis and the
negative frequencies occupy the right half. The figure shows how MatLab plots the
spectrum.
140

Magnitude spectrum of sinusoid


f = 10 Hz N = 256sample

120

100

80

60

Nyqvist frequency

40

20

Positive frequencies

50

100

Negative frequencies

150

200

250

The frequency with index n = 1 is zero. The numbers will be displaced by 1 step by plotting
the spectrum. We have to introduce a positive frequency axis numbered fro 0 to N/2 - 1.
We can use the earlier defined variable n to number the frequency axis.

Signals in MatLab page 1

A function that generate a sinusoid can look like this:


function

x=signal(f,N)

% sinusoid
% f=frequency, N=number of samples
% Total sampling time = 1 second
n = 0:N-1; % numbering the samples
x = sin(2*pi*f*n/N);
plot(x)
MFx = abs(fft(x));
plot(n(1:N/2),MFx(1:N/2)) % plot positive frequencies

Another way to present a spectrum is to use the command fftshift,


which center the frequencies around f = 0 that will be numbered N/2.
MFx = fftshift(abs(fft(x))); plot(MFx)
140

Magnitude spectrum of sinusoid


f = 10 Hz N = 256sample

120

100

80

60

40

20

Negative frequencies

50

Positive frequencies

100

150

200

250

The signal and the spectrum can be plotted together with the commands:
subplot(2,1,1); plot(x)subplot(2,1,2);
plot(n(1:N/2),MFx(1:N/2))

1
0.5
0
-0.5
-1

50

100

150

200

250

50

100

150

200

250

150

100

50

Signals in MatLab page 2

If we want to study the real- and imaginary part of the spectrum, we can extract these parts
by the commands:
RFX = real(Fx);

IFX = imag(Fx);

In the example we have been sampling during 1 sec and the frequency resolution is 1 Hz.
If we specify the sampling frequency to fs, which frequency resolution will we have?
With N sample the total sampling period is T = N/fs.
The frequency resolution df = 1/T = fs/N. Due to the symmetrical properties of the spectrum
we only use the positive frequencies.
The signal will be: x = sin(2*pi*f*n/fs);
The frequency scale is: frek = (0:N/2-1)*fs/N;
and we can plot the positive frequencies with:

plot(frek,MFx(1:N/2))

A function that generates a sinusoid with the sampling frequency fs can look like this:
function
%
%
%
%

x=signal(f,fs,N)

sinusoid
f=frequency, N=number of samples
fs=sampling frequency
Total sampling time = 1 second

n = 0:N-1; % numbering the samples


x = sin(2*pi*f*n/fs);
plot(x)
MFx = abs(fft(x));
frek=(0:N/2-1)*fs/N;
plot(frek,MFx(1:N/2))% plot the positive frequencies

you can add amplitude and phase angle to the function, and also determine how many
samples that will be plotted:
Function sinewave(N,f,fs,np)
% sinwave calculate and plot a sinusoid with N sample
% sampling frequency fs, frequency f and plot np
% samples
t=(0:N-1)/fs;
x = sin(2*pi*t*f);
plot(t(1:np),x(1:np))

Signals in MatLab page 3

Here is a suggestion how to generate some other signal in MatLab.


A square wave is generated by: y = sign(sin(2*pi*f*n/N));
Another function is square that also can generate asymmetrical pulse trains.
A sawtooth signal can be generated by: sawtooth(2*pi*f*n/N,0.5);
Some other functions are: pulstran, rectpuls, tripuls, gauspuls

Signals in MatLab page 4

Exporting signals in MatLab to.wav-files


.wav-files is a standard audio format in PC computers that can be handled by many programs.
MatLab can read and write .wav-files wit the following commands:

[x,Fs,bits] = wavread(filename) reads the file filename.wav to the vector x in


MatLab. Fs and bits extracts the sampling frequency and the bit resolution of the sampled signal.

x = wavread(filename,N) reads the first N samples.


wavwrite(x,Fs, filename) writes the vector x in a file with samplingfrequency Fs.
The standard resolution is 16 bits or 32768 levels. The amplitude is restricted to the interval [-1,+1]
in the vector x.

wavwrite(x,Fs,bit, filename)changes the resolution to bit bits.


Assume we have following parameters:
N = samples, fs = sampling frequency, fa = frequency of the .wav-file

n = 0:N-1 numbers the samples


A sinusoid will have the right frequency with the command: y = sin(2*pi*fa*n/fs);
Example: Generate a sinusoid with amplitude 0.1, frequency 100 Hz with 100000 samples and
sampling frequency 22050 Hz. Listen to the signal and save it to a .wav-file.
Total time for the signal is N/fs = 4.535 s.

N = 100000; fs = 22050; fa = 100; n = 0:N-1;


x = 0.1*sin(2*pi*fa*n/fs);
sound(x,fs)
wavwrite(x,fs,sinsound.wav);

What will the frequency be if we have a .wav-signal stored in the variable x?


If we take M samples of the signal, the total sampling time will be: T = M/fs
The frequency resolution is: df = 1/T = fs/M
Magnitude spectrum is: Fx = abs(fft(x))
We can generate a frequency axis for positive frequencies: frek=(0:M/2-1)*fs/N
Plotting the spectrum: plot(frek,Fx(1:M/2))

Signals in MatLab page 5