0
votes

Need help since kind of lost on this. I am trying to graph the code below i have in which I made a white noise and used STFT to do the bandpass filtering but now I need to graph the signal in to two graphs for each channel. The result should be graphs. For graph 1a., the horizontal axis should be frequency, the vertical axis should be amplitude. For (2).b, the horizontal axis should be time, the vertical axis should be frequency, and express the amplitude using color.

function newwhitenoise()
L = 5000; %Sample length for the random signal
Pause = 10000; %Sample Pause Gap
mu = 0;
sigma = 2;

%Need to see left signal is not displaying
Left_signal = sigma*randn(L,1) + mu;
Right_signal = sigma*randn(L,1) + mu;

Long_signal = [Left_signal zeros(L,1); zeros(Pause,2); zeros(L,1) Right_signal];

%Player Object
soundRecord(Long_signal);


disp([Left_signal zeros(L,1)]);
%sound(Long_signal, Fs);

%Plots subplots in graph
%figure
%subplot(211);
%plot(Left_signal, 'b'); grid on;
%subplot(212);
%plot(Right_signal, 'r'); grid on;
end

function signalplayer(signal)
    %load(signal);
    fs = 44100; %Sample Frequency
    obj = audioplayer(signal,fs);
    play(obj);
end

function soundRecord (signal)
fs = 44100; %Sample Frequency
recObj = audiorecorder(44100, 16, 2);
get(recObj)

%save sound to wave file
%filename = 'location.flac';
audiowrite('input.wav',signal, fs);


if ~exist('inFile')
    inFile = 'input.wav';
end

if ~exist('outFile')
    outFile = 'output.wav';
end

if ~exist('frameWidth')
    frameWidth = 4096;          % size of FFT frame, better be a power of 2
end
frameHop = frameWidth/2;

analWindow = hanning(frameWidth);

[inBuffer, Fs] = wavread(inFile);

x = [inBuffer(:,1); linspace(0, 0, frameWidth)'];                   % use left channel only, zeropad one frame at the end

clear inBuffer;

numSamples = length(x);

numFrames = floor(numSamples/frameHop)-1;

% disp(frameWidth);
% disp(numSamples);
% disp(frameHop);
% disp(numFrames);
% disp(size(analWindow));
% disp(size(transpose(analWindow)));

y = linspace(0, 0, numSamples)';


n = 0;                              % init sample pointer.  unlike MATLAB, i like counting from 0

for frameIndex = 1:numFrames

     xWindowed = x(n+1:n+frameWidth) .* analWindow;     % get and window the input audio frame

     X = fft(fftshift(xWindowed));              % do the FFT

     Y = X;                         % copy the input spectrum to output

                                % do whatever processing to Y that you like

     yWindowed = fftshift(real(ifft(Y)));           % convert back to time domain, toss the imaginary part
 %disp(size(x(1:frameWidth)));
 %disp(size(yWindowed));

     y(n+1:n+frameWidth) = y(n+1:n+frameWidth) + yWindowed;

     n = n + frameHop;
end

wavwrite(y, Fs, 'output.wav');
1
For graph 2, you can use Matlab's built-in spectrogram() function; see "help spectrogram". For graph 1, you probably want the average along time of each frequency bin of the array returned by spectrogram().dpwe
@dpwe is it possible to use the spectrogram() function on the signal before it goes thru STFT and after or can I only use it after?Asau7610
spectrogram() is performing an STFT; you apply it to the original time-domain waveform. You can use the output of spectrogram as the STFT matrix instead of having to write your own segment-window-overlap loop.dpwe

1 Answers

0
votes

For graph 1 try pwelch and for graph 2 try spectrogram.
pwelch is basically the average of the squared magnitude of the STFT. The spectrogram function returns the STFT of the signal thus it operates on the signal in the time domain.
Both functions are called with the same input parameters and when called with no output, plot the result on the current axes.
If you want the frequency axis to be the vertical (y) in graph 2, use the option 'yaxis' in the call for spectrogram. I suggest you look at the documentation of both functions.