Thursday, January 14, 2010

Plotting a Discrete Time-Domain Signal in Matlab



In order to properly construct a sinusoid in Matlab we need to do a few things. We need to give Matlab the equation of our sinusoid. We also need to tell Matlab how often to sample the sinusoid and for how long we want to sample our sinusoid. Essentially, we want to tell Matlab “ok, watch this 5Hz sinusoid for 1 second. In that one second, take 50 discrete samples (actually 51), and plot the data. I should see a point every 0.02 seconds, for a total of 51 total points (Matlab starts at zero).” What we want to do may seem pretty simple but this process often confuses students. This overview will help in understanding the proper construction of sinusoids. I say “proper” because we will use the proper DSP terms when we tell Matlab what to do. For example, we will use terms like “sample frequency” and “sample period” to describe our signal.

To establish our signal we will need to:


  1. Set the sample frequency FS
  2. Set the sample period Ts
  3. Set the time window (number of samples)
  4. Set the sample index Tn (time index)
  5. Establish the signal x(t) and plot

1. Set the Sample Frequency (Fs):

This is our sample frequency. This is basically how many “points” we will use to construct each signal. Remember it’s zero based, so it’s really plus one. In the example below, Fs was set to 50, so we have 51 points in our plot, we ‘sample’ the signal 51 times. This is a 5Hz signal, so we are sampling it 10 times per cycle, which satisfies Nyquist-Shannon’s sampling theorem.


Matlab code:

Fs = 50;





2. Set the Sample Period (Ts):

Next, we will set the sample period, Ts. The sample period is calculated as 1/Fs, or 1/50 in this example = 0.02. This is essentially the ‘granularity’ of our sampling. In other words we will have a discrete point every 0.02 units (which will be time). If we zoom into our signal and only look at the first 0.2 seconds, we can see that we have 11 datapoints, one data point every 0.02s (remember it’s zero based).


Matlab code:
Ts = 1/Fs;


3. Set the Time Window (n):

Next we will establish out how many points are in one second of our signal. We will setup a matrix from zero to one second. In our example Fs = 50, so n = 0:1*Fs. When we set n in Matlab, all we are really doing is setting up a matrix of numbers from 0 to 50. That is: 0,1,2,3,4,5,6,…..,50. We will use this matrix to set the sample index Tn in the next step.

n is now equal to the matrix below:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50


Matlab code:
n =0:1*Fs;


4. Establish the Sample Index (Tn):

Now we will tell Matlab how to chop up or signal. We set the Sample Index as Tn = Ts * n. In our example, Tn will equal .02. We want to see a discrete point every 0.02 seconds. We will take n, or matrix from 0 to 500 and multiply it by Ts which was 0.02. Now we have a matrix with 51 elements from 0 to 1 spaced every 0.02 seconds. We are now ready to plot our sinusoid.

Tn is now equal to the matrix below:

0 0.0200 0.0400 0.0600 0.0800 0.1000 0.1200 0.1400 0.1600 0.1800 0.2000 0.2200 0.2400 0.2600 0.2800 0.3000 0.3200 0.3400 0.3600 0.3800 0.4000 0.4200 0.4400 0.4600 0.4800 0.5000 0.5200 0.5400 0.5600 0.5800 0.6000 0.6200 0.6400 0.6600 0.6800 0.7000 0.7200 0.7400 0.7600 0.7800 0.8000 0.8200 0.8400 0.8600 0.8800 0.9000 0.9200 0.9400 0.9600 0.9800 1.0000


Matlab code:

Tn =Ts*n;


5. Plot our 5Hz Sinusoid:

Plotting our sinusoid is pretty easy at this point. Remember, we want a 5Hz cosine signal constructed of 51 points in one second; we want to “watch” the signal for 1 second and plot the results. We can plot the X axis correctly with time also using tn.


clear all; %clear all variables

fs = 50; % sample freq

Ts=1/fs; % sample period

n=0:1*fs; % time window (fs=samples per second), 1 second

tn=n*Ts; % sample index

sig_a=cos(2*pi*5*tn);
figure('Color',[1 1 1]);

stem(tn,sig_a);

title('Stem Plot Example: Fs = 50 (50 Points)');

axis('tight');



Friday, January 8, 2010

DSP Tutorial Matlab Script



The script below will get you started plotting basic time domain signals. The following tutorials are available on additional DSP topics:





This is a simple Matlab script which describes the basic plotting of time-domain signals.

%% DSP Tutorial Script
% James Eastham
% Member, IEEE
% Created on: 01/08/2010
% Revision: R1

%% Plotting a Basic Time Domain Signals
% Sinewave with a peak amplitude of 1 and a fequency of freq
clear all;
close all;
freq = 5; %5Hz
n=0:.01:.2; %number of discrete values on X axis
x = cos (2*pi*freq*n); %our cosine signal
figure('Color',[1 1 1]);
plot(n,x);
title('Example of Tim-Domain Signal');
xlabel('Time(s)');
ylabel('Amplitude');

%% Plotting a Basic Time Domain Signal Continued
figure('Color',[1 1 1]);
stem(n,x,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','Markersize',10);
hold on;
plot(n,x);
title('Example of Time-Domain Signal Plot');
xlabel('Time(s)');
ylabel('Amplitude');


%% Plotting with the Subplot Feature
% subplot(m,n,p) breaks the figure window into an
% m-by-n matrix
figure('Color',[1 1 1]);
subplot(2,1,1);
plot(n,x);
title('Example of the Time-Domain Signal');
xlabel('Time(s)');
ylabel('Amplitude');
ns=1:length(n);
subplot(2,1,2);
stem(ns,x,'.r');
title('Example of the Descrete Points');
xlabel('Descrete Sample');
ylabel('Amplitude');
axis([1 21 -1.2 1.2]);
grid on;

Thursday, November 19, 2009

Modeling Temperature in LTSpice - Part I

We can model temperature behavior in LTSpice using the built in step function.

To model a list of temps, insert the spice directive ".step temp list "
example: .step temp list -25 -10 10 20 25 50 75

To model a sweep, insert the spice directive ".step temp "
example: .step temp -55 125 25

The following example models a simple transistor amplifier from -55C to 125C in 25C steps.



The transient analysis below shows the source current over temperature.

Saturday, November 7, 2009

Modeling Diode Reverse Breakdown Voltages in LTSpice

The included models in LTSpice do not include reverse breakdown. In order to model the reverse breakdown "vrev" can be specified in the diode model. For this example, create a new schematic and call the diode "mydiode". Insert the .model directive to add the reverse breakdown. Below, I used all the standard parameters for the 1N4148 and added vrev=50. The vrev specifies the reverse breakdown at -50V. Many other diode properties can be defined in the model including the non-linear characteristics of the breakdown.

Forward and reverse breakdown and current limitations can be modeled by using a more sophisticated model. Using the model below, forward and reverse current limiting can be observed.

.model mydiode D(vrev=40 Ibv=20nA BV=40 Ibv=1e-10 Is=1e-7 Rs=10 Tt=2n Ilimit=5mA Vpk=40 Revilimit=5mA)

Friday, October 16, 2009

Adding a LM741 Op Amp Model to LTSpice

Simulating operational amplifiers in LTSpice using non-ideal characteristics is desirable for students to understand op amp AC and DC limitations. Many student projects require the use of the classic LM741 op amp. The following steps can be followed to get the LM741 model into LTSpice for simulation.

Step 1: Obtain the spice model for the LM741 (see complete model below)

Step 2: Copy this model into the LTSpice sub directory. The file must be given a *.sub extension and not a *.txt extension. Make sure to save the file as “all file types” with the .sub extension. Save the file as LM741.sub. The directory is typically: C:\Program Files\LTC\LTspiceIV\lib\sub

Step 3: Start up LTSpice, insert the op amp2 component. Right click on the symbol and change the value to LM741.

















Step 4: Add the spice directive to the schematic using the .op. Add “.lib LM741.sub” to the schematic.

















Step 5: You are now ready to run simulations with the LM741.













Spice model below:


*//////////////////////////////////////////////////////////////////////
* (C) National Semiconductor, Inc.
* Models developed and under copyright by:
* National Semiconductor, Inc.

*/////////////////////////////////////////////////////////////////////
* Legal Notice: This material is intended for free software support.
* The file may be copied, and distributed; however, reselling the
* material is illegal

*////////////////////////////////////////////////////////////////////
* For ordering or technical information on these models, contact:
* National Semiconductor's Customer Response Center
* 7:00 A.M.--7:00 P.M. U.S. Central Time
* (800) 272-9959
* For Applications support, contact the Internet address:
* amps-apps@galaxy.nsc.com

*//////////////////////////////////////////////////////////
*LM741 OPERATIONAL AMPLIFIER MACRO-MODEL
*//////////////////////////////////////////////////////////
*
* connections: non-inverting input
* | inverting input
* | | positive power supply
* | | | negative power supply
* | | | | output
* | | | | |
* | | | | |
.SUBCKT LM741 1 2 99 50 28
*
*Features:
*Improved performance over industry standards
*Plug-in replacement for LM709,LM201,MC1439,748
*Input and output overload protection
*
****************INPUT STAGE**************
*
IOS 2 1 20N
*^Input offset current
R1 1 3 250K
R2 3 2 250K
I1 4 50 100U
R3 5 99 517
R4 6 99 517
Q1 5 2 4 QX
Q2 6 7 4 QX
*Fp2=2.55 MHz
C4 5 6 60.3614P
*
***********COMMON MODE EFFECT***********
*
I2 99 50 1.6MA
*^Quiescent supply current
EOS 7 1 POLY(1) 16 49 1E-3 1
*Input offset voltage.^
R8 99 49 40K
R9 49 50 40K
*
*********OUTPUT VOLTAGE LIMITING********
V2 99 8 1.63
D1 9 8 DX
D2 10 9 DX
V3 10 50 1.63
*
**************SECOND STAGE**************
*
EH 99 98 99 49 1
G1 98 9 5 6 2.1E-3
*Fp1=5 Hz
R5 98 9 95.493MEG
C3 98 9 333.33P
*
***************POLE STAGE***************
*
*Fp=30 MHz
G3 98 15 9 49 1E-6
R12 98 15 1MEG
C5 98 15 5.3052E-15
*
*********COMMON-MODE ZERO STAGE*********
*
*Fpcm=300 Hz
G4 98 16 3 49 3.1623E-8
L2 98 17 530.5M
R13 17 16 1K
*
**************OUTPUT STAGE**************
*
F6 50 99 POLY(1) V6 450U 1
E1 99 23 99 15 1
R16 24 23 25
D5 26 24 DX
V6 26 22 0.65V
R17 23 25 25
D6 25 27 DX
V7 22 27 0.65V
V5 22 21 0.18V
D4 21 15 DX
V4 20 22 0.18V
D3 15 20 DX
L3 22 28 100P
RL3 22 28 100K
*
***************MODELS USED**************
*
.MODEL DX D(IS=1E-15)
.MODEL QX NPN(BF=625)
*
.ENDS
*$[/url]

Saturday, August 29, 2009

Getting Started with LTSpice


LTSpice is a great free circuit simulator. The software and introduction videos can be found at: www.cmosedu.com

Sunday, June 14, 2009

New Mac Mini

I finally took the plunge and picked up a Mac. I'm recording a brief diary of pros/cons from my journey: