📄 pcm.m
字号:
for i=1:num
i=int2str(i);
i=strcat('n',i);
r.(i)=[];
end
s=num*s; % Normalized the signal.
mins=min(s); % The minimun value in the signal
maxs=max(s); % The maximum value in the signal
cb=linspace(mins,maxs,num); % Creates the codeblocks from mins,
% to maxs with num divisions
cbd=[0:(length(cb)-1)]; % Code Created only with symbolic propueses, to
%make easier the plotting process of the digital signal because it
%doesn't have negative values.
e=100; % Initialize the error.
% Quantize the Signal with the algorithm described in the book
% Telecommunications Demystified.
for i= 1:limit;
for j= 1:length(s)
for k = 1:num
temp(k)=(s(j)-cb(k))^2;
end
index=find(temp==min(temp));
index=index(1);
numind=index;
index=int2str(index);
index=strcat('n',index);
r.(index)=horzcat(r.(index),s(j));
squan(j)=cb(numind);
squand(j)=cbd(numind);
if (i==1)
first=squan;
end
end
err=(s-squan).^2;
e=mean(err);
if (e<limit/100)
break;
end
for k = 1:num
index=int2str(k);
index=strcat('n',index);
cbn(k)=mean(r.(index));
end
cb=cbn;
end
set(handles.text5,'Visible', 'on'); % Makes visible the text5
set(handles.text6,'Visible', 'on'); % Makes visible the text6
set(handles.text6,'String', e); % Puts e on text6
axes(handles.axesquan) % Select the proper axes
plot(t,s,t,first,'g',t,squan,'r'); % Plots the quantized signal
xlabel('Time');
ylabel('Amplitude');
legend('Original Signal','First Quantization','After Quantization');
grid on;
handles.quan=squan;
handles.quand=squand;
handles.nbit=num;
handles.cbd=cbd;
handles.cb=cb;
guidata(gcbo,handles);
elseif (get(handles.radiobutton8,'Value') == get(handles.radiobutton8,'Max'))
s=handles.signal;
maxs=max(s);
t=handles.time;
[pdfs,xp]=ksdensity(s);
index=1;
for i=1:length(xp)
if (xp(i)<0 & xp(i+1)>0)
index=i;
break
end
end
pdfs=pdfs(index:end);
xp=xp(index:end);
pdfs=pdfs.^3;
p=polyfit(pdfs,xp,6);
psym=poly2sym(p);
g=int(psym);
gn=sym2poly(g);
gn=polyval(gn,t);
maxgn=max(gn);
thefact=maxs/maxgn;
gn=gn*thefact;
axes(handles.axesquan) % Select the proper axes
plot(t,gn);
xlabel('Time');
ylabel('Amplitude');
legend('Best G(x) that minimizes the mse')
grid on;
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in radiobutton8.
function radiobutton8_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.edit2,'Enable', 'off');
set(handles.edit3,'Enable', 'off');
set(handles.text5,'Visible', 'off');
set(handles.text6,'Visible', 'off');
% Hint: get(hObject,'Value') returns toggle state of radiobutton8
% --- Executes on button press in radiobutton7.
function radiobutton7_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.edit2,'Enable', 'on');
set(handles.edit3,'Enable', 'on');
% Hint: get(hObject,'Value') returns toggle state of radiobutton7
% --- Executes during object creation, after setting all properties.
function pushbutton5_CreateFcn(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cb=handles.cb;
cbd=handles.cbd;
squand=handles.quand;
nbit=handles.nbit;
nbit=round(sqrt(nbit));
lsquand=length(squand);
for i=1:lsquand
sbin(i,:)=de2bi(squand(i),nbit);
end
figure
hold on
[m,n]=size(sbin);
liminf=0;
s1={'Quantized Signal','Symbol','Digital Signal'};
bins=dec2bin(cbd,nbit);
xlswrite('Output',s1,'Hoja1','A1');
xlswrite('Output',cb','Hoja1','A2');
xlswrite('Output',cbd','Hoja1','B2');
xlswrite('Output',bins,'Hoja1','C2');
nconst=n;
for i = 1:m
sbint=sbin(i,:);
timet=[liminf:n-1];
if rem(i,2)==0
stem(timet, sbint,'r');
else
stem(timet, sbint,'k');
end
liminf=n;
n=n+nconst;
end
grid on
% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
helpdlg('In order to understand this program in a deeper way, it is desirable to have the book Telecommunications Demystified of Carl Nassar, it is possible to have an electronic version of the book. You can work with the chapter four of the book.', 'Recomendations');
% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -