%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Hw3_Pb1.m % ELEC-E5610 - Acoustics and the Physics of Sound % November 2020 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Problem: % Given the initial displacement of a plucked undamped string (see % below), create the spectrum of the resulting vibration. %% String parameters: L = 1; % string length d = 999; % no. of spatial steps x = 0:L/d:L-L/d; % spatial axis, divides L into d steps h = 0.01; % plucking amplitude p = 4; % plucking point variable (actual plucking location is L/p) y0 = zeros(1,d); % create string displacement vector %% set initial string shape (triangular string, plucked at L/p): for n = 1:d if x(n) <= L/p y0(n) = p*h/L*x(n); else % L/p < x <= L y0(n) = p*h/(p-1)*(1-x(n)/L); end end % draw string displacement: close all; figure(1); plot(x,y0); title('Initial string shape'); xlabel('String length coordinate'); ylabel('String displacement'); %% create the spectrum Fs = 48000; % sampling frequency Y = zeros(1,Fs/2); % initialize the spectrum f0 = 440; % fundamental frequency (Hz) nmax = floor((Fs/2)/f0); % highest representable mode number (Nyquist theorem) %%%%%%%%%%% INSERT YOUR OWN CODE HERE %%%%%%%%%%% for n = 1:nmax Y(n*f0) = getNthStringModeAmplitude(n,h,p,L,x); % spectrum of the string vibration end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Plot the spectrum: figure(2); hold on epsilon = 1e-20; % a small constant to be added to the spectrum so that log(0) is avoided when plotting below Y = Y./max(Y); % normalize spectrum to 0 dB plot(20*log10(abs(Y+epsilon))); % plot spectrum title('Resulting spectrum'); grid on; % plot grid for clarity xlabel('Frequency (Hz)'); % name the x-axis ylabel('Amplitude (dB)'); % name the y-axis axis([0 Fs/2 -100 0]); % zoom in