# function for sampling sine function

I wanted to write a function for sampling sine function with sampling frequency sf,frequency f and length l, so I wrote the function adopting from the sampling formula, what change should I apply to my function? or is there any better way to do so?

function X = ssin(f,sf,l) %f as frequency, sf as sampling frequency, l as length

End=l/f;

t = 1/f:1/f:End;

d = 1/f:1/sf:End;

pt = pulstran(t,d,'rectpuls');

ms = sin(f.*t);

X = pt.*ms;

stem(X)

end

Is there a specific reason you need to use pulstran() ? Since you have an explicit expression for your signal, you can just sample the sine wave at the specified times as follows:

function x = ssin(f0,Fs,N)

% f0 = sine wave frequency [Hz]
% Fs = sampling frequency [Hz]
% N = number of samples

dt = 1/Fs;       % Sampling period [s]
T = N*dt;        % Record window duration [s]
t = 0:dt:(T-dt); % Time vector for sampling [s]

% generate samples at the specified times
x = sin(2*pi*f0*t); % [signal units]

stem(t,x)

end

% run the function
x = ssin(1,20,60)


• Thank you sir, No I've just read the sampling topic and I was trying to make a general function working just like the main "sampling" formula, but seems like I was putting the cart before the horse! Apr 14 '20 at 10:43