# how to generate sinusoid in matlab

Status
Not open for further replies.

#### moonnightingale

##### Full Member level 6 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)

#### moonnightingale

##### Full Member level 6 can any body help me

#### andre_luis

##### Super Moderator
Staff member 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 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 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 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 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.