Skip to content

Instantly share code, notes, and snippets.

@dangpzanco
Last active April 13, 2017 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dangpzanco/5f830e3c66ec557f1e04ffef3f8898e9 to your computer and use it in GitHub Desktop.
Save dangpzanco/5f830e3c66ec557f1e04ffef3f8898e9 to your computer and use it in GitHub Desktop.
Clipping plot example
clc
close all
clearvars
N = 10000;
n = (0:N-1)';
fs = 80000;
fc = [20, 80];
amp = [0.5, 0.25];
phase = [0 -pi];
x = zeros(N,1);
for i=1:length(fc)
x = x + amp(i)*cos(2*pi*fc(i)/fs*n + phase(i));
end
x = x/max(abs(x));
clip_level = 0.25;
% Mc*x = Mc*y
yc = x;
yc(yc >= clip_level) = nan;
yc(yc <= -clip_level) = nan;
% Ms-*x <= Ms-*y
ylc = x;
ylc(abs(ylc) < clip_level) = nan;
ylc(ylc >= clip_level) = nan;
ylc(ylc <= -clip_level) = -clip_level;
% Ms+*x >= Ms+*y
yuc = x;
yuc(abs(yuc) < clip_level) = nan;
yuc(yuc >= clip_level) = clip_level;
yuc(yuc <= -clip_level) = nan;
x(abs(x) <= clip_level) = nan;
ptau = '$$ +\tau $$';
mtau = '$$ -\tau $$';
ytick = [-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1];
plot(n,yc,'k-','linewidth',1.25); hold on
plot(n,yuc,'r-.','linewidth',1.25)
plot(n,ylc,'g-.','linewidth',1.25)
plot(n,x,'b--','linewidth',1.25)
xlabel('Samples','FontSize',12)
ylabel('Amplitude','FontSize',12)
set(gca,'YTick',ytick);
set(gca,'FontSize',12);
text(N+50,clip_level,ptau,'Interpreter','latex','FontSize',14);
text(N+50,-clip_level,mtau,'Interpreter','latex','FontSize',14);
legend({'$\boldmath{M}_c\boldmath{y}$','$\boldmath{M}_s^+\boldmath{y}$',...
'$\boldmath{M}_s^-\boldmath{y}$','$\boldmath{x}$'},...
'Interpreter','latex','FontSize',14,'Position','SouthEast')
zoom off; zoom xon;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment