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


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

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

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

ms = sin(f.*t);

X = pt.*ms;




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]



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

enter image description here

  • 1
    $\begingroup$ 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! $\endgroup$ Apr 14 '20 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.