How do I plot output of function symclip?

조회 수: 4 (최근 30일)
Andrew Smith
Andrew Smith 2021년 4월 18일
답변: Pratyush Roy 2021년 5월 12일
I am attemtping to recreate the m-file functions used by Zolzer in his Digital Audio Effects book. He has an M-file called symclip as shown below and I have included a recorded audio file to apply the clipping effect to. However, I cannot plot the output.
function y=symclip(x);
[x, fs] = audioread('E NOTE.wav');
% y=symclip(x)
% "Overdrive" simulation with symmetrical clipping
% x - input
N=length(x) ;
th=1/3; % threshold for symmetrical soft clipping
% by Schetzen Formula
for i = 1:N
if abs(x(i))< th, y(i)=2*x(i);end;
if abs(x(i))>=th,
if x(i)> 0, y(i)=(3-(2-x(i)*3) .^2)/3; end;
if x(i)< 0, y(i)=-(3-(2-abs(x(i))*3) .^2)/3; end;
end ;
if abs(x(i))>2*th,
if x(i)> 0, y(i)=1;end;
if x(i)< 0, y(i)=-1;end;
end ;
end;
plot(N, y)
Any help would be greatly appreciated

답변 (1개)

Pratyush Roy
Pratyush Roy 2021년 5월 12일
Hi Andrew,
According to the code, x is passed as the input argument to the function. However, according to the second line of the code, we get to know about the value x only after we have read the file usin audioread. So we can pass the name of the file as input to the function instead of the MATLAB array containing the sound intensity values.
Another change that can be made to the code is to use the command plot(1:N, y) instead of plot(N,y) since both the arguments for the plot function should be arrays whereas in the latter case it is a value N versus an array y.
You can refer to the code snippet below:
function y=symclip(filename)
[x, fs] = audioread(filename);
N=length(x) ;
th=1/3; % threshold for symmetrical soft clipping
% by Schetzen Formula
for i = 1:N
if abs(x(i))< th, y(i)=2*x(i);end;
if abs(x(i))>=th,
if x(i)> 0, y(i)=(3-(2-x(i)*3) .^2)/3; end;
if x(i)< 0, y(i)=-(3-(2-abs(x(i))*3) .^2)/3; end;
end ;
if abs(x(i))>2*th,
if x(i)> 0, y(i)=1;end;
if x(i)< 0, y(i)=-1;end;
end ;
end;
plot(1:N, y)
Hope this helps!

카테고리

Help CenterFile Exchange에서 Audio Processing Algorithm Design에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by