Changing frequency of a signal in Matlab - matlab

I have a difficulty in solving one Matlab problem. I want to read a wav file, Re-arrange the data so that the frequency is quadrupled and play the file. Here is my first attempt, am I doing the correct thing? Any help would be appreciated.
hfile = 'one.wav';
wavwrite(y, Fs, hfile)
clear y Fs
[y, Fs, nbits, readinfo] = wavread(hfile);
sound(y, Fs); % Play the sound & wait until it finishes
duration = numel(y) / Fs; % Calculate the duration
pause(duration + 2) % Wait that much + 2 seconds
FsQ=Fs*4;
sound(y,FsQ,nbits)

Related

MATLAB FFT waveform plot with detrend and shift only has one spike

I have some beginner, basic Physionet data I am trying to apply FFT to, but I'm a bit confused by the results I'm getting and don't think they're correct. I'm mostly using code from this example and from the fftshift documentation page with some tweaks but I'm not too sure what's wrong with my code or results. I have attached my code and snapshots of the results I'm getting.
load aami3am.mat
Fs = 720; % Sampling frequency
T = 1/Fs; % Sample time
L = 60000; % Length of signal
t = (0:L-1)*T; % Time vector
plot(t(1:43081),val(1:43081))
title('aami4b_h Signal')
xlabel('Seconds')
ylabel('ECG Amplitude')
NFFT = 2^nextpow2(L);
val = detrend(val);
Y = fft(val,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y(1:NFFT/2+1)))
title('FFT')
yY = fftshift(Y);
fshift = (-L/2:L/2-1)*(Fs/L);
powershift = abs(yY).^2/L;
plot(fshift(1:L),powershift(1:L))
title('FFT Shifted')
I'm using 43081 because there are 43081 values in the .mat file over the 60 seconds of data.

Remove noise from mp3 file, MATLAB

I have a mp3 file in the link below, where there is a man human voice and some humming noise in the background. I want the humming noise removed. Is there anyone who can tell me how to do it in MATLAB?
https://www.dropbox.com/s/h95y1oelbzvcgkc/allthatbass.mp3?dl=0
%% Read in the file
clearvars;
close all;
[f,fs] = audioread('allthatbass.mp3');
%% Play original file
pOrig = audioplayer(f,fs);
N = size(f,1);
%% Plot the spectrum
df = fs / N;
w = (-(N/2):(N/2)-1)*df;
y = fft(f(:,1), N) / N; % For normalizing, but not needed for our analysis
y2 = fftshift(y);
figure;
plot(w,abs(y2));
%% Design a bandpass filter that filters out between 700 to 12000 Hz
n = 7;
beginFreq = 700 / (fs/2);
endFreq = 12000 / (fs/2);
[b,a] = butter(n, [beginFreq, endFreq], 'bandpass');
%% Filter the signal
fOut = filter(b, a, f);
%% Construct audioplayer object and play
p = audioplayer(fOut, fs);
p.play;
I expect the humming noise removed, but the output sound like the original sound.
enter link description here
I have downloaded the original file you linked in your question and added the following line at the end:
audiowrite('filtered.wav', fOut, fs);
The resulting file 'filtered.wav' sounds very different to my ears (I used a headset to listen). If you open 'filtered.wav' for example in Audacity and take a look at the spectrum then it looks indeed different from the original (as expected, the frequencies below 700 Hz and above 12 kHz are removed).
Let's try to verify this in matlab. The following script reads both files and plots the dB value of both ffts. The lower plot represents the filtered signal and it is clearly visible that the bass frequencies are removed. The cut above 12 kHz is visible as well but it seems that these frequencies have already been attenuated in the original signal and the bandpass filter reinforces that.
%% Read in both files
clearvars;
close all;
[f,fs] = audioread('allthatbass.mp3');
[fflt, fsflt] = audioread('filtered.wav');
N = size(f,1);
%% Compute the ffts
df = fs / N;
n = N / 2; % plot only the second half of the spectrum
w = (0:(n)-1)*df;
y = fft(f(:,1), N) / N;
y2 = fftshift(y);
yflt = fft(fflt(:,1), N) / N;
y2flt = fftshift(yflt);
%% Plot the spectrum of both files (use the dB value, i.e. 10 * log(abs(x)) )
figure;
ax1 = subplot(2,1,1);
plot(w,10*log(abs(y2(n:end-1,1))));
ax2 = subplot(2,1,2);
plot(w, 10*log(abs(y2flt(n:end-1,1))));
linkaxes([ax1, ax2], 'y'); % link the axes (this makes it easier to visually compare the plots)
Try using a low pass filter in MATLAB. They're relatively easy to implement. You can find the documentation, along with examples, here.

Converting audible sound to ultrasonic

I have sound clips that are about 3-4 seconds in human audible range. I want to convert them into ultrasonic range so that when I transmit them they are not audible to humans. I read that I need to use amplitude modulation. I used the modulate function of matlab.
[y,Fs] = audioread('TakeASelfie.mp3');
x = modulate(y,30700, 62000, 'amdsb-tc');
soundsc(x,62000)
audiowrite('modulated.wav', x, 62000)
In the above example, I was trying to convert my audio clip to 30.7kHz. However, after I performed modulation, the length of the clip was decreased. How can I change the frequency of my sound clip without changing the length of it? I am also not sure if the approach I am taking is the right one.
One way to do it (this method will give you lowwer sideband, for example if your audio signal have spectra from 200 to 2000 Hz and carrier frequency f0 = 40000 Hz, your ultrasound signal will have spectra from 39800 to 38000 Yz):
fs=96000; % samplig frequency (should be at least x2.2 of f0 )
[sb, fd]=wavread('as4.wav'); % signal in audio domain
if fd ~= fs % change sampling prequency to target
sb = resample(sb, fs, fd);
end
sb=sb./max(sb); % normalization
A = 1; % amplitude of carrier tone
T = length(sb) / fs; % length of signal
f0 = 40000; % carrier frequency
Fi0 = pi/2; % initial phase
N = T * fs; % number of samples
t = (0 : N-1) / fs; % time stamps
sa = A * sin(2 * pi * f0 * t + Fi0); % samples of carrier tone
% first method
s = sb .* sa + imag(hilbert(sb)) .* imag(hilbert(sa));
% to have upper sideband use
% s = sb .* sa - imag(hilbert(sb)) .* imag(hilbert(sa));
% second method
s = ssbmod(sb, f0, fs);
s=s./(max(s)); % normalization of modulated signal

Find the minimum bandwidth that can be used octave

i should find the minimum bandwidth that can be used for the song to still be distinguishable by a listener
and this is my code so far
% A.Read the song and assign vules
[y, fs] = wavread('Dog Woof.wav');
time = (1:length(y))/30000;
plot(time, y); % ploting the sin wave of the song
title('Sound waves');
xlabel('Time');
ylabel('Frequency');
% B.Simple rate will be the fs value
fs
% C.Bandwidth
[UPPER, LOWER] = bandwidth(y)

How do i mathematically apply a window function to a signal in matlab?

I am currently trying to make a plot of a windowed signal. The plot is currently been made in latex, but can't seem to recreate the plot generated in matlab in latex.
%% Time specifications:
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 60; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%% Sine wave:
Fc = 60; % hertz
x = sin(0.9*pi*t) + sin(0.42*pi*t);
%x = cos(2*pi*Fc*t) + cos(2*pi*Fc*5*t)+ sin(2*pi*Fc*10*t);
% Plot the signal versus time:
figure;
%plot(t,x);
%xlabel('time (in seconds)');
%title('Signal versus Time');
%zoom xon;
%%
window = hamming(length(t),'periodic');
%plot(window)
windowed = x.*window;
plot(windowed)
This generates a plot like this
But when i tries to recreate the plot in matlab.. I get something way different.
What I am plotting is this :
w= sin(0.9*pi*t) + sin(0.42*pi*t)*0.54 - 0.46*( cos(360*t/600))
plot(w)
First part being the signal * window = windowed signal...
And what I get is this..
Why is this so different.. What am I doing wrong?
As I see it you have two errors in your expression. First you miss parenthesis around the window function and second you have the wrong period in the cos term. Try
w = (sin(0.9*pi*t) + sin(0.42*pi*t)).*(0.54 - 0.46*( cos(2*pi*t/t(end))));