Signal_Final_TikeaTE/sampling.m

49 lines
1.1 KiB
Matlab

function [t,x] = sampling(freq_signal, sampling_freq, phase_signal, duration, plt)
%{
function [t,x] = sampling(freq_signal, sampling_freq, phase_signal, duration, plt)
ex: [t,x] = sampling(10,30,1,1)
Task: generate a cosine function knowing frequency, sampling freq, phase and duration
Inputs:
-freq_signal: frequency of the signal (in hz)
-sampling_freq: sampling frequency (how many sample per second) (in hz)
-phase_signal: signal phase (in rad)
-duration: how long u want the signal to be
plt: if greater than 0, signal will be plotted
Outputs:
-t: time vector
-x: amplitude of the cosine signal
-a figure of a cosine signal
Author: Tikea TE
Date: 11/04/2025
%}
% generate the time vector
t = -duration/2: 1/sampling_freq: duration/2;
% generate the cosine signal
x = cos(2*pi*freq_signal*t + phase_signal);
% plotting the figure
if(plt)
figure;
plot(t,x,'b');
xlabel('Time(s)');
ylabel('signal amplitude(a.u.)');
title(['cosine signal at ', num2str(freq_signal), 'Hz']); % use [] for joining string
grid on;
end