Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

how to generate sinusoid in matlab

Status
Not open for further replies.

moonnightingale

Full Member level 6
Joined
Sep 17, 2009
Messages
362
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Activity points
3,832
Hi i want to geenrate a sinusoid of .5 Khz with sampling frequency of 6 Khz
kindly help me to plot it

---------- Post added at 13:13 ---------- Previous post was at 12:42 ----------

ok i have written this code can anybody verify it

amp = 2; % Define the amplitude
freq_HZ = 500; % Define the frequency in Hertz
freq_RPS = 2*pi*freq_HZ; % Compute the frequency in rad/sec
t = 0:0.000166:1; % Define the sampling frequency
y = amp * sin(freq_RPS * t); % Compute y(t), the sine wave
plot(t,y) % Plot y vs. t
xlabel('Time (seconds)') % Label x-axis
ylabel('amplitude') % Label y-axis
title('Sinusoid of .5 Khz with fs=6Khz') % Give plot a title
grid % Turn on plot grid
figure
%plotting original signal with stem
stem(t,y)
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,200
Helped
1,142
Reputation
2,303
Reaction score
1,119
Trophy points
1,403
Location
Brazil
Activity points
53,585
Try that :

t = [ 0 : 1 : 40 ]; % Time Samples
f = 500; % Input Signal Frequency
fs = 6000; % Sampling Frequency
x = sin(2*pi*f/fs*t); % Generate Sine Wave
figure(1);
stem(t,x,'r'); % View the samples
figure(2);
stem(t*1/fs*1000,x,'r'); % View the samples
hold on;
plot(t*1/fs*1000,x); % Plot Sine Wave
 

enren

Newbie level 6
Joined
Nov 1, 2009
Messages
14
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,283
Location
France
Activity points
1,351
I made a quick generic fonction.
Parameters are :
1: signal frequency
2: amplitude
3: phase
4: sampling frequency
5: number of period to display


function [ ] = singen(freq, amp, phi, fs, nT)
clf;

T=nT/freq;
t=0:1/fs:T;

x=amp*sin(2*pi*freq*t+phi);

plot(t, x);
xlabel('Time (seconds)');
ylabel('Amplitude');
h = strcat({'Sinusoid of '}, num2str(freq), {'Hz with fs = '}, num2str(fs), {' Hz'});
title(h), grid on;
end
 

moonnightingale

Full Member level 6
Joined
Sep 17, 2009
Messages
362
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Activity points
3,832
thanks andre.
but is there some problem in my code.

my book question says

DFT

(a) generate a sinusoid 05 .5 Khz with sampling freq 6 Khz
(b) plot the original signal using stem

I think first two parts are done. the third part says abt complex exponentials

can u also help me to write matlab code to compute its complex exponentials. This is related to DFT
thanks a lot
 

permute

Advanced Member level 3
Joined
Jul 16, 2010
Messages
923
Helped
295
Reputation
590
Reaction score
268
Trophy points
1,343
Activity points
8,543
sin = e^jwt - e^-jwt
cos = e^jwt + e^-jwt

This is why the FFT of a sine wave generates significant content at Fc and (Fs-Fc). A complex input will have different magnitudes for at least some of the content in the frequencies between Fs/2 and Fs. (these correspond to -Fs/2 to 0).
eg:
Code:
f = sin(2*pi*100/1000 * [0:1023]);  % 1024 samples of 100hz sampled at 1khz
fd = f .* exp(-i*50/1000 * [0:1023]));  
fu = f .* exp(+i*50/1000 * [0:1023])); 
figure(); plot(20*log(abs(fft(f))));
figure(); plot(20*log(abs(fft(fd))));
figure(); plot(20*log(abs(fft(fu))));
in the above, you will see a peak near 100hz and near 900hz (aka -100Hz). The second plot, the peaks move to 50hz and 850Hz (aka -150Hz). the third plot moves the peaks to 150Hz and 950Hz (aka -50Hz). Also, in all three plots there will be more than just a single line -- 100 hz @ 1khz sampling rate doesn't end at a full cycle for 1024 samples. Thus it is not a basis vector (a frequency that exactly corresponds to a frequency bin).
 

moonnightingale

Full Member level 6
Joined
Sep 17, 2009
Messages
362
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Activity points
3,832
sin = e^jwt - e^-jwt
cos = e^jwt + e^-jwt

This is why the FFT of a sine wave generates significant content at Fc and (Fs-Fc). A complex input will have different magnitudes for at least some of the content in the frequencies between Fs/2 and Fs. (these correspond to -Fs/2 to 0).
eg:
Code:
f = sin(2*pi*100/1000 * [0:1023]);  % 1024 samples of 100hz sampled at 1khz
fd = f .* exp(-i*50/1000 * [0:1023]));  
fu = f .* exp(+i*50/1000 * [0:1023])); 
figure(); plot(20*log(abs(fft(f))));
figure(); plot(20*log(abs(fft(fd))));
figure(); plot(20*log(abs(fft(fu))));
in the above, you will see a peak near 100hz and near 900hz (aka -100Hz). The second plot, the peaks move to 50hz and 850Hz (aka -150Hz). the third plot moves the peaks to 150Hz and 950Hz (aka -50Hz). Also, in all three plots there will be more than just a single line -- 100 hz @ 1khz sampling rate doesn't end at a full cycle for 1024 samples. Thus it is not a basis vector (a frequency that exactly corresponds to a frequency bin).
Thanks Permute but is it matlab code to compute its complex exponentials of sinusoif of 0.5 Khz with sampling frequency of 6 Khz
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top