function y=data_ch(x,snr,ID);
%	y=data_ch(x,snr,ID)
% Simulates a data channel.
% x(1) is the clock period in samples,
% x(2:length(x)) is the input signal,
% snr is the power signal to noise ratio.
% Channel type is governed by ID:
% 	1 Butterworth of order 6
% 	2 linear phase FIR
% 	3 Chebychev type I with 3 db of ripple in passband
% 	4 Butterworth with phase distortion
% If ID is negative, the channel filter will be displayed.
% y=[clock period,channel output vector]

	L=2048;fs=24000;ts=1/fs;fc=1500;T=L*ts;
	if abs(ID)==1
			[bc,ac]=butter(6,2*fc/fs);
	end
	if abs(ID)==2
		ac=1;
		v=2*pi*fc*ts*[1:30];v=sin(v)./v; % construct a
		bc=2*ts*fc*[flipud(v')',1,v]; % linear phase
		bc=bc.*hamming(length(bc))';%  FIR baseband filter
	end 
	if abs(ID)==3
		[bc,ac]=cheby1(6,3,2*fc/fs);
	end	
	if abs(ID)==4
			[bc,ac]=butter(6,2*fc/fs);
			[b0,a0]=butter(4,fc/fs);
			ac=conv(ac,a0);
			bc=conv(bc,flipud(a0')');
	end	

    if ID >0
		clk=x(1);L=length(x)-1;t=ts*[0:L-1];
		s=x(2:L+1);nbits=floor(L/clk);
% pass signal plus noise through the channel
		n=(1/sqrt(snr))*randn(1,L); % make noise
		y=filter(bc,ac,s+n);
		figure,subplot(2,1,1)
		plot(t,s/max(s)+1.5,'r',t,y/max(y)-1.5,'k')
		axis([0 T -3 3])
		axis('off')
		ttl='data channel in and out signals;  ';
		ttl=[ttl,num2str(T),' seconds, ',num2str(nbits)];
		ttl=[ttl,' symbols, clock period = ',num2str(clk),' samples'];
		title(ttl)
		subplot(2,2,3)
		spectrm(y,fs)
		title(['(output spectrum) fc = ',num2str(fc),' Hz'])
		subplot(2,2,4)
		y=[clk,y]; % disclose clock period
		eye_diag(y)
		title(['channel ',num2str(ID),',  data rate = ',num2str(fs/(clk*fc)),'*fc,    SNR = ',num2str(snr)])
	else
		figure,plotZTP(bc,ac,0,60)
		y=[x(1)]; % Output the clock cycle
	end