function [s, t] = csinplot(amp,fre,phs,trange, fs, arange)
% This fucntion creates a sinudoid based on the following input 
% parameters:
%
%  [s, t] = csinplot(AMP, FRE, PHS, TRANGE, FS, ARANGE)
%
%  AMP is the amplitude of the sinusoid
%  FRE is the frequency of the sinusoid in Hz
%  PHS is the phase of the sinusoid in radians
%  TRANGE is a 2 point vector for the start and stop points in seconds
%  FS is the sampling frequency of the sine in samples per second (usually
%  at least 20 points per period will make for a smooth plot, therefore, FS
%  should be 10 times bigger than FRE).
%  ARANGE is a 2 point vector that clips the waveform in amplitude.  The lower
%  point first and then the higher.  If you don't want clipping, set the ARANGE
%  points bigger than the signal range.
%
%  This output will be a labeled plot in a figure window, and the signal
%  vector S with time axis points T in seconds.
%  Written by Kevin D. Donohue 9/23/04 (donohue@engr.uky.edu)

%  Create Time axis:

tindex = (round(trange(1)*fs):round(trange(2)*fs));  % convert time range to
                                                     % integer samples
t = tindex/fs;   %  Divide by sampling frequency to get actual time sample values

s = amp*sin(2*pi*fre*t+phs);  % Compute unclipped sine points

%  Apply clip from below
lowindex = find(s < arange(1));  % Find the index values of all sample points below low clip 
if ~isempty(lowindex)  %  if points exist in this range ...
    s(lowindex) = arange(1);         %  Assign all points below, to the clip value
end

%  Apply clip from above
highindex = find(s > arange(2));  % Find the index values of all sample points above high clip 
if ~isempty(highindex)   %  if points exist in this range ...
    s(highindex) = arange(2);         %  Assign all points above, to the clip value
end

%  Comment out lines below to not plot result in function
figure   %  Open a new figure window
plot(t,s)
xlabel('Seconds')
ylabel('Amplitude')
title(['Sine waveform at ', num2str(fre) , ' Hertz'])
