Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need matlab code for sine function

Status
Not open for further replies.

rizwan12

Junior Member level 1
Joined
Aug 20, 2013
Messages
19
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
116
HI everyone.

I want to plot a signal of sine wave with a frequency of 0.1 hz with an amplitude of -50 to 50 and time interval of 0 to 3500.can any anoe help me plz
regards
Rizwan
 

When you say time interval of 3500, does that mean you have 3500 time steps per cycle? Then your frequency of 0.1 Hz is a cycle every ten seconds, and each time step is .00286 seconds.

Here is how I would write a routine in BASIC programming language.

(2 * Pi is one cycle in radian-based trig calculations as commonly used in computer routines.)


Code Basic4GL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
FOR div = 0 to 3500
 
A = 2 * Pi * div / 3500
 
B = SINE ( A )
 
amplitude = B * 50
 
elapsedtime = 10 * div / 3500
 
PRINT div, elapsedtime, amplitude
 
NEXT div



Of course you will scale the plot values to fit your screen.
 

Hiya Rizwan,

Like Brad, I'm not sure what you mean by a "time interval of 0 to 3500"..? If I interpret your requirement as "for a time interval of 3500 [seconds]", then a suitable MATLAB snippet could be:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
tD = 3500;      % [s]  Time duration
f  = 0.1;       % [Hz] Sine frequency
A  = 50;        % [-]  Dimensionless peak amplitude
 
Fs = 10;        % [Hz] Sampling frequency
 
t = 0:(1/Fs):(tD-1/Fs);  % Construct a vector of sampling 'instants' over the specified duration
 
sinOut = A*sin(2*pi*t*f);
 
% Plot it!
figure;          
plot (t, sinOut);
ylim([-A-10 A+10]);
title(['A ' num2str(f) ' Hz sine wave over ' num2str(tD) ' seconds.']);
xlabel('Time [seconds]')
ylabel('Amplitude');
grid on;



The only extra thing I had to specify was a 'sampling frequency' (Fs) - or how often we calculate (and plot) a point over your 3500 second interval. The Nyquist criterion demands that Fs must be at least twice the signal of interest (0.1 Hz), so while anything > 0.2Hz would work, I chose 10 [Hz] to make it all look pretty (and PC memory's cheap ;)

To zoom in on the plot without disturbing your vertical scale (which I've set to something aesthetically pleasing via the ylim([ ]) command), right click in the figure and select 'Horizontal zoom only'.

Cheers :)
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top