Audio spectrum, pitch shift and generic audio elaboration in MATLAB - matlab

I'm writing a MATLAB program which takes the fundamental (example, la4 or A4 at 440Hz), and, after having asked to the user the frequency which frequency he/she wants, the program will produce the transposed sound. If, for example the user wants a 880Hz frequency from the original file which is 440Hz, it will double every frequency (harmonics included).
I have tried to use MIRPitch, which is not implemented here, but the function wasn't working anyway after having installed all the toolboxes.
How can I take the fundamental? How can I transpose it in other ways?
[y,Fs]=audioread("Test.wav");
[x,Fs]=audioread("LookingForHappiness.mp3");
D=0.8;
%D=(1/Fs)*length(x);
% Digital format ----------------------------------------------------------
Fs=44100; % Sampling rate (Hz)
Ts=1/Fs; % Sampling Period (s)
% Start DFT -------------------------------------------------------------
W=D; % Set the window analysis to pattern duration
N=W/Ts; % Samples in a window frame
F0=1/W; % Fourier fundamental frequency
K=Fs*W; % Total computable frequencies
for k=0:K-1 % k is the frequency index (f=K*F0)
for(n=0:N-1)
s(n+1)=sin(2*pi*k*F0*n*Ts); % n is the time index (t=n*Ts)
c(n+1)=cos(2*pi*k*F0*n*Ts); % n is the time index (t=n*Ts)
end
Xi(k+1)=0;
Xr(k+1)=0;
for (n=0:N-1)
Xi(k+1)=Xi(k+1)+x(n+1)*s(n+1);
Xr(k+1)=Xr(k+1)+x(n+1)*c(n+1);
end
Xi(k+1)=Xi(k+1)/N; % Scale by 1/N each frequency bin
Xr(k+1)=Xr(k+1)/N; % Scale by 1/N each frequency bin
end
Xi(1)=Xi(1)/2; % Frequency 0 amplitude is scaled by 1/N
Xr(1)=Xr(1)/2; % Frequency 0 amplitude is scaled by 1/N
% Show results ------------------------------------------------------------
Xa=2*sqrt(Xi.^2+Xr.^2); % Computes amplitude spectrum
Fscale=0:F0:(Fs/2);
subplot(2,1,1);
plot(Fscale,Xa(1:K/2+1)); % Plot the amplitude spectrum
xlabel ('frequency (Hz) ')
subplot(2,1,2);
plot(x(1:2)); % Plot the signal %era 1:100
xlabel ('time (s) ')
sound("L4A.mp3");

Related

How to use trapz to calculate a triple integral on a spherical harmonic?

I have a combination of spherical harmonics.
Because spherical harmonics are an orthogonal basis, we can say:
Now, I have a function that gives me a spherical harmonic, which gives a spherical harmonic matrix. (the famous spharm4)
First, I want to check if the Y_6^2 is normalized (the integral should be equal to zero) using trapz. How can a 3D integral be done with trapz function using spherical coordinates?
After that, I want to find the coefficient b using eq(2), but I still can’t understand how to use the trapz function correctly and multiply the matrices.
Any help would be appreciated.
Function that gives me a spherical harmonic:
function varargout=plm2spec(lmcosi,norma,in,ot)
% [sdl,l,bta,lfit,logy,logpm]=PLM2SPEC(lmcosi,norma,in,ot)
%
% Calculates the power spectrum of real spherical harmonic
% sine and cosine coefficients contained in the matrix 'lmcosi' which is
% of the standard form that can be plotted by PLM2XYZ and PLOTPLM.
%
% INPUT:
%
% lmcosi Spherical harmonic coefficients [l m Ccos Csin]
% norma 1 multiplication by (l+1)
% This gives the mean-square value of the
% gradient of a potential in Schmidt-harmonics
% 2 division by (2*l+1) [default]
% This gives the proper power spectral density
% as we've come to know it
% 3 none, i.e. a scaling factor of 1
% in Index to minimum degree to consider for the spectral fit [defaulted]
% ot Index to maximum degree to consider for the spectral fit [defaulted]
%
% OUTPUT:
%
% sdl Spectral density: energy per degree
% l Degree
% bta Spectral slope of loglog(l,sdl)
% lfit,logy Spectral line plot given by loglog(lfit,logy)
% logpm Error on spectral line plot given by
% loglog(lfit,logpm)
%
% SEE ALSO: MTVAR
%
% EXAMPLE:
%
% [sdl,l,bta,lfit,logy,logpm]=plm2spec(fralmanac('EGM96'));
%
% SEE ALSO: ACTSPEC
%
% The normalization by (2l+1) is what's required when the spherical
% harmonics are normalized to 4pi. See DT p. 858. A "delta"-function then
% retains a flat spectrum. See Dahlen and Simons 2008.
% See papers by Hipkin 2001, Kaula 1967, Lowes 1966, 1974, Nagata 1965
% (Lowes, JGR 71(8), 2179 [1966])
% (Nagata, JGeomagGeoel 17, 153-155 [1965])
%
% Last modified by fjsimons-at-alum.mit.edu, 03/18/2020
defval('norma',2)
lmin=lmcosi(1);
lmax=lmcosi(end,1);
pin=0;
for l=lmin:lmax
clm=shcos(lmcosi,l);
slm=shsin(lmcosi,l);
pin=pin+1;
sdl(pin)=clm(:)'*clm(:)+slm(:)'*slm(:);
end
switch norma
case 1
normfac=(lmin:lmax)+1;
case 2
normfac=1./(2*(lmin:lmax)+1);
case 3
normfac=1;
disp('Not further normalized')
otherwise
error('No valid normalization specified')
end
% disp(sprintf('Normalization %i',norma))
sdl=normfac.*sdl;
sdl=sdl(:);
% Figure out the range over which to make the fit
l=lmin:lmax;
l=l(:);
if lmin==0
defval('in',3);
elseif lmin==1
defval('in',2);
else
defval('in',1);
end
defval('ot',lmax)
lfit=l(in:ot);
if nargout>=3
% Calculate spectral slope
[bt,E]=polyfit(log10(lfit),log10(sdl(in:ot)),1);
bta=bt(1);
[logy,loge]=polyval(bt,log10(lfit),E);
logy=10.^logy;
logpm=[logy./(10.^loge) logy.*(10.^loge)];
else
[bta,lfit,logy,logpm]=deal(NaN);
end

Matlab FM Demodulation and Get Rid of Phase Folding Effect

I have a matlab code to Frequency modulation and demodulation a signal. My code is work well for modulation part. My message signal is m and modulated signal is u, code plot the message signal and its integral in one graph for plotting 1.
Then signal modulated with carrier and program plots the modulated signal in time domain for plotting 2.
After that, by the help of some code blocks program find the frequency spectrum of modulated signal and message signal to plot graph of them for plotting 3.
In demodulation part program make some fundamental calculation for FM detection, then to obtain message signal it uses filter.
Last part program plots the graph of recovered signal with message signal to compare them.
I summarized all code because ı do not know whre is the problem.
My problem about plotting 3 when I make zoom graph 3 I see some phase foldings or like it.Graph is not symmetric according to y-axis.
I did not solve this problem, I research about them and I decided to use unwrap(). Although I tried a lot, I could not be successful. How can I get rid of this phase folding with unwrap() function. Thank you.
My matlab code is ;
ts = 0.0001;% Sampling interval
t0 = 0.15; % Duration
t = 0:ts:t0;% define time vector
%% OTHER PARAMETERS
fc = 200; % Carrier signal frequency
kf =50; % Frequency deviation constant
fs = 1/ts; % Sampling frequency
%% MESSAGE SIGNAL SIMPLY
m = 1*(t<t0/3)-2*(t<2*t0/3).*(t>=t0/3);
%% Integration of m
int_m(1) = 0;
for k =1:length(m)-1
int_m(k+1) = int_m(k) + m(k)*ts;
end
%% PLOTTING 1
figure; subplot(211); % Message signal
plot(t,m);grid on;xlabel('time');ylabel('Amplitude');
title('m(t)');ylim([-3 2]);
subplot(212);plot(t,int_m);% Integral of message signal
grid on; xlabel('time');ylabel('Amplitude');title('integral of m(t)');
ylim([-0.07 0.07]);
%% FM MODULATED SIGNAL
u = cos(2*pi*fc*t + 2*pi*kf*int_m);
%% PLOTTING 2
figure; plot(t,u); % Modulated signal in time domain
grid on;xlabel('time');
ylabel('Amplitude');title('FM :u(t)');
ylim([-1.2 1.2]);
%% FINDING FREQUENCY SPECTRUM AND PLOTTING 3
% Frequency spectrum of m(t)
f=linspace(-1/(2*ts),1/(2*ts),length(t));
M=fftshift(fft(m))./length(t); % Taking fourier transform for m(t)
U=fftshift(fft(u))./length(t); % Taking fourier transform for u(t)
figure;subplot(211); % Frequence spectrum of m(t)
plot(f,abs(M)); grid;
xlabel('Frequency in Hz');xlim([-500 500]);
ylabel('Amplitude');title('Double sided Magnitude spectrum of m(t)');
subplot(212);plot(f,abs(U)); % Frequency spectrum of u(t)
grid;xlabel('Frequency in Hz');xlim([-500 500]);
ylabel('Amplitude');title('Double sided Magnitude spectrum of u(t)');
%% DEMODULATION (Using Differentiator)
dem = diff(u);
dem = [0,dem];
rect_dem = abs(dem);
%% Filtering out High Frequencies
N = 80; % Order of Filter
Wn = 1.e-2; % Pass Band Edge Frequency.
a = fir1(N,Wn);% Return Numerator of Low Pass FIR filter
b = 1; % Denominator of Low Pass FIR Filter
rec = filter(a,b,rect_dem);
%% Finding frequency Response of Signals
fl = length(t);
fl = 2^ceil(log2(fl));
f = (-fl/2:fl/2-1)/(fl*1.e-4);
mF = fftshift(fft(m,fl)); % Frequency Response of Message Signal
fmF = fftshift(fft(u,fl)); % Frequency Response of FM Signal
rect_demF = fftshift(fft(rect_dem,fl));% Frequency Response of Rectified FM Signal
recF = fftshift(fft(rec,fl)); % Frequency Response of Recovered Message Signal
%% PLOTTING 4
figure;subplot(211);plot(t,m);grid on;
xlabel('time');ylabel('Amplitude');
title('m(t)');ylim([-3 2]);
subplot(212);plot(t,rec);
title('Recovered Signal');xlabel('{\it t} (sec)');
ylabel('m(t)');grid;
My problem is in this third graph to show well I put big picture
k = -(length(X)-1)/2:1:length(X)/2;
Your k is not symmetric.
If you work with symmetric k is it working fine?

Do I need to call fftshift before calling fft or ifft?

In the book "Computational Fourier Optics, A Matlab Tutorial" by David Voelz, it is written that a call to fftshift is needed before a call to fft or ifft, but in the MATLAB documentation of fftshift it's only written that this command
rearranges the outputs of fft, fft2, and fftn by moving the zero-frequency component to the center of the array.
There is no mention in documentation that this command should be called before the call to fft, and I saw some examples that call fft without a call to fftshift beforehand.
My question is: Whether or not fftshift needed to be called before a call to fft or ifft?
If fftshift doesn't need to be called before a call to fft, and only after a call to fft, then when should we use (if at all) the command ifftshift with relation to a calculation of the fft of a data set?
The matlab fft only computes half of the frequency spectrum (positive frequencies and the zero frequency if your number of samples is odd) in order to save computation time.
Then, the second half of the frequency spectrum (which is the complex conjugate of the first half) is just added at the end of this vector.
So what you get after a fft is the following vector:
0 1 2 3 ... Fs/2 -Fs/2 ... -3 -2 -1
<----------------> <------------------>
positive freq negative freq
where Fs is the frequency sample.
Now, what fftshift does is just shifting the negative frequency bins (2nd part of the frequency spectrum) at the beginning of your vector so that you can display a nice frequency spectrum starting at -Fs/2 and ending at +Fs/2.
The shifted vector becomes:
-Fs/2 ... -3 -2 -1 0 1 2 3 ... Fs/2
<------------------> <---------------->
negative freq positive freq
So, to answer your question, no you don't need to use fftshift after or before a call to fft or ifft. But if you used a fftshift on your vector, you should undo it by applying an ifftshift or fftshift. (I think both calls are equivalent.)
If you read onwards in the documentation on fftshift:
"It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum."
The shift is not necessary to perform the fft, but it is handy to visualise the Fourier transform. Whether to use fftshift or not is thus dependent upon whether you want to visualise your transform or not.
Note that ifftshift is different than fftshift because it shifts the negative back to the positive. Assume a simple 3 bin unit impulse in frequency domain before fftshift
[0, exp(-jw1), exp(-(jw1-pi)=exp(-jw1+pi)];
fftshift gives
[exp(-jw1+pi)], 0, exp(-jw1)];
you can see the discontinuity in phase. If you perform ifftshift, negative freq is shifted back to positive:
[0, exp(-jw1), exp(-jw1+pi)];
whereas, fftshift again gives:
[exp(-jw1), exp(-jw1+pi), 0];
one can see the phase monotonicity is not the same, (aka, fftshift-ifftshift case gives [decrease, increase] and fftshift-fftshift case gives [increase, decrease] in phase).
Given the title of the book you are studying, I assume you are working with images. Then the proper way is to call both fftshift and ifftshift before and after you call [i]fft.
Your code should look like
spectrum = fftshift(fft2(ifftshift(myimage))
It is quite the same when applying the inverse Fourier transform
myimage = fftshift(ifft2(ifftshift(spectrum))
The simple answer is call to fftshift not needed before a call to fft. fftshift does not influence the calculation of Fast fourier transform. fftshift rearranges values inside a matrix. For eg:
cameraman = imread('cameraman.tif');
fftshifted_cameraman = fftshift(cameraman);
subplot(1,2,1); imshow(cameraman); title('Cameraman');
subplot(1,2,2); imshow(fftshifted_cameraman); title('FFTShifted Cameraman');
It depends on what you are going to do with the transformed data. If you don't perform an fftshift before transforming, the fft result will have every other value multiplied by -1. This doesn't matter if you plan to view the magnitude or magnitude squared of the result. However, if you plan to compare adjacent spectral values and phase is important, you'll need to apply fftshift before transforming to avoid the alternating sign.
Here is a MATLAB script I use to test basic sound analysis functions of MATLAB including fftshift() in displaying the output of fft().
if ~exist('inputFile', 'var')
inputFile = 'vibe.wav';
end
[inputBuffer, Fs] = audioread(inputFile);
fileSize = length(inputBuffer);
numSamples = 2.^(ceil(log2(fileSize))); % round up to nearest power of 2
x = zeros(numSamples, 1); % zero pad if necessary
x(1:fileSize) = inputBuffer(:,1); % if multi-channel, use left channel only
clear inputBuffer; % free this memory
clear fileSize;
t = linspace(0, (numSamples-1)/Fs, numSamples)';
f = linspace(-Fs/2, Fs/2 - Fs/numSamples, numSamples)';
X = fft(x);
plot(t, x);
xlabel('time (seconds)');
ylabel('amplitude');
title(['time-domain plot of ' inputFile]);
sound(x, Fs); % play the sound
pause;
% display both positive and negative frequency spectrum
plot(f, real(fftshift(X)));
xlabel('frequency (Hz)');
ylabel('real part');
title(['real part frequency-domain plot of ' inputFile]);
pause;
plot(f, imag(fftshift(X)));
xlabel('frequency (Hz)');
ylabel('imag part');
title(['imag part frequency-domain plot of ' inputFile]);
pause;
plot(f, abs(fftshift(X))); % linear amplitude by linear freq plot
xlabel('frequency (Hz)');
ylabel('amplitude');
title(['abs frequency-domain plot of ' inputFile]);
pause;
plot(f, 20*log10(abs(fftshift(X))+1.0e-10)); % dB by linear freq plot
xlabel('frequency (Hz)');
ylabel('amplitude (dB)');
title(['dB frequency-domain plot of ' inputFile]);
pause;
% display only positive frequency spectrum for log frequency scale
semilogx(f(numSamples/2+2:numSamples), 20*log10(abs(X(2:numSamples/2)))); % dB by log freq plot
xlabel('frequency (Hz), log scale');
ylabel('amplitude (dB)');
title(['dB vs. log freq, frequency-domain plot of ' inputFile]);
pause;
semilogx(f(numSamples/2+2:numSamples), (180/pi)*angle(X(2:numSamples/2))); % phase by log freq plot
xlabel('frequency (Hz), log scale');
ylabel('phase (degrees)');
title(['phase vs. log freq, frequency-domain plot of ' inputFile]);
pause;
%
% this is an alternate method of unwrapping phase
%
% phase = cumsum([angle(X(1)); angle( X(2:numSamples/2) ./ X(1:numSamples/2-1) ) ]);
% semilogx(f(numSamples/2+2:numSamples), phase(2:numSamples/2)); % unwrapped phase by log freq plot
%
semilogx(f(numSamples/2+2:numSamples), unwrap(angle(X(2:numSamples/2)))); % unwrapped phase by log freq plot
xlabel('frequency (Hz), log scale');
ylabel('unwrapped phase (radians)');
title(['unwrapped phase vs. log freq, frequency-domain plot of ' inputFile]);
If you were windowing segments of audio and passing them to the FFT, then you should use fftshift() on the input to the FFT to define the center of the windowed segment as the t=0 point.
[x_input, Fs] = audioread('vibe.wav'); % load time-domain input
N = 2*floor(length(x_input)/2); % make sure N is even
x = x_input(1:N);
t = linspace(-N/2, N/2-1, N); % values of time in units of samples
omega = linspace(-pi, pi*(1-2/N), N); % values of (normalized) angular frequency
X = fftshift( fft( fftshift( x.*hamming(length(x)) ) ) );
[X_max k_max] = max( abs(X) );
figure(1);
plot(t, x, 'g');
figure(2);
plot(omega, abs(X), 'b');
hold on;
plot(omega(k_max), X_max, 'or');
hold off;

Frequency spectrum of signal in Matlab

Here is the code I use to plot a function in frequency domain in Matlab:
dt = 1/10000; % sampling rate
et = 0.1; % end of the interval
t = 0:dt:et; % sampling range
y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.*180.*t); % sample the signal
subplot(2,1,1); % first of two plots
plot(t,y); grid on % plot with grid
xlabel('Time (s)'); % time expressed in seconds
ylabel('Amplitude'); % amplitude as function of time
Y = fft(y); % compute Fourier transform
n = size(y,2)/2; % 2nd half are complex conjugates
amp_spec = abs(Y)/n; % absolute value and normalize
subplot(2,1,2); % second of two plots
freq = (0:100)/(2*n*dt); % abscissa viewing window
stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum
xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second
ylabel('Amplitude'); % amplitude as function of frequency
The problem is, when I zoom in my graph I don't see peaks exactly on 50Hz, 90Hz and 180Hz.
What did I do wrong in my code?
The problem is that in order to get perfect spectra (peaks on 50,90,180 and 0 otherwise), your interval should be a multiplier of all the frequencies.
Explanation: consider y=sin(2.*pi.*t). Use your code to plot it with:
1) et = 1-dt;
2) et = 1;
In the first case, if you zoom in, you will see that peak is perfectly on 1 Hz. In the second case it is not (also very close).
Why? Because you are working with the finite number of points, and, consequently, finite number of frequencies. If 1 Hz is among this set of frequencies (1st case), you will get perfect peak at 1 Hz at zeros at all others frequencies.
In case 2 there is no 1 Hz frequency in your set, so you will get peak on the nearest frequencies (and also it will have finite width).
Ultimately, in your original code, there are no 50, 90, 180 Hz frequencies in your full set of frequencies.

on the use and understanding of pwelch in matlab

I'm using the pwelch method in matlab to compute the power spectra for some wind speed measurements. So, far I have written the following code as an example:
t = 10800; % number of seconds in 3 hours
t = 1:t; % generate time vector
fs = 1; % sampling frequency (seconds)
A = 2; % amplitude
P = 1000; % period (seconds), the time it takes for the signal to repeat itself
f1 = 1/P; % number of cycles per second (i.e. how often the signal repeats itself every second).
y = A*sin(2*pi*f1*t); % signal
fh = figure(1);
set(fh,'color','white','Units', 'Inches', 'Position', [0,0,6,6],...
'PaperUnits', 'Inches', 'PaperSize', [6,6]);
[pxx, f] = pwelch(y,[],[],[],fs);
loglog(f,10*(pxx),'k','linewidth',1.2);
xlabel('log10(cycles per s)');
ylabel('Spectral Density (dB Hz^{-1})');
I cannot include the plot as I do not have enough reputation points
Does this make sense? I'm struggling with the idea of having noise at the right side of the plot. The signal which was decomposed was a sine wave with no noise, where does this noise come from? Does the fact that the values on the yaxis are negative suggest that those frequencies are negligible? Also, what would be the best way to write the units on the y axis if the wind speed is measured in m/s, can this be converted to something more meaningful for environmental scientists?
Your results are fine. dB can be confusing.
A linear plot will get a good view,
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
y = sin(2 * pi * 50 * t); % 50Hz signal
An fft approach,
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
subplot(1,2,1);
plot(f,2*abs(Y(1:NFFT/2+1)))
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
pwelch approach,
subplot(1,2,2);
[pxx, freq] = pwelch(y,[],[],[],Fs);
plot(freq,10*(pxx),'k','linewidth',1.2);
xlabel('Frequency (Hz)');
ylabel('Spectral Density (Hz^{-1})');
As you can see they both have peak at 50Hz.
Using loglog for both,
So "noise" is of 1e-6 and exists in fft as well, and can be ignored.
For your second question, I don't think the axis will change it will be frequency again. For Fs you should use the sampling frequency of wind speed, like if you have 10 samples of speed in one second your Fs is 10. Higher frequencies in your graph means more changes in wind speed and lower frequencies represent less changes for the speed.