1
$\begingroup$

I have a wav file recorded from my smartphone's mic, and I want to reconstruct the sampled signal and plot the reconstructed signal. After some research and search, I was able to get the following code:

[y, fs, nb] = wavread('new.wav');
x=[1:1:length(y)];
z = y(:,[1]);
samp=transpose(z);
b = 48000/fs;

% Quantizing the Sampled Signal
    no = N/b;           % no. of samples
    z = ceil(5*(no));       % no. of bits required
    quan = zeros(1,z);      % constructing a zero array
    c = (abs(p_max) + abs(p_min))/32     % size of quantized level
    u = p_min;
    w = 1;
    quant = zeros(1,N);
    for (q = 1 : b : N)
        for (r = 1 : 32)
            y = r;
            if (samp(q)>=u & samp(q)<=(u+c))
                for (v = (w+4) : -1 : w) % this loop is for converting decimal into binary
                    quan(v) = mod(y,2);
                    y = (y/2)- (mod(y,2)/2);
                end
                 quant(q) = r;
            end
            u = u + c;
        end
        u = p_min;
        w = w + 5;
    end
    quan

    % Writing the Quantized Signal
    fid = fopen ('quant.bin','w');
    count = fwrite (fid,quant,'int8');

    % Reading the Quantized Signal
    fid = fopen ('quant.bin','r');
    [quant_r,count] = fread (fid,inf,'int8');

    % Reconstructing the Sample of the Quantized Signal
    for (n = 1 : N)
        u = p_min;
        for (ab = 1 : 32)
            if (quant_r(n)==ab)
                quant_r(n) = u;
            end
            u = u + c;
        end
    end
    figure
    stem (quant_r);
    title ('The Reconstructed Sample of Quantized Signal')
    grid on;

    % Reconstructing the Original Signal
    for (c1 = 1 : b : (N-b))
        if (b~=1)
            xyz = (quant_r(c1+b)-quant_r(c1))/b;
            for (c2 = (c1+1) : (c1+b-1))
                quant_r(c2) = quant_r(c2-1) + xyz;
            end
        end
    end
    figure
    quant_r = smooth (quant_r);
    plot (quant_r);
    title ('The Reconstructed Signal')
    grid on;

But it looks like I can't get my desired output, I'm completely new to signal processing, can someone tell me what is wrong with my code? Thank you!

Update#1: I just realized that I used a fairly high sample rate(44100hz), so I tried downsampling using the following code:

[piano,fs]=wavread('new.wav'); % loads piano
play=piano(:,1); % Renames the file as "play"

%t = linspace(0,time,length(play));          % Time vector
x = play;
y = decimate(x,25);

stem(x(1:30)), axis([0 30 -2 2])   % Original signal
title('Original Signal')
figure
stem(y(1:30))                        % Decimated signal
title('Decimated Signal')

%changes the sampling rate

fs1 = fs/2;
fs2 = fs/3;
fs3 = fs/4;
fs4 = fs*2;
fs5 = fs*3;
fs6 = fs*4;

wavwrite(y,fs3,'new1');

Is this what I should do? Thank you!

Update#2: I think my problem is this line: b = 48000/fs; it should be sampled interval, I have no idea how to deal with this.

Update#3: I just found the following code that seems do the same thing, is this right? If so I don't need to implement it myself.

sub1=wavread('jamming.wav');
 magnSub1=abs(fft(sub1));
 phaseSub1=angle(fft(sub1));
 sub1_L=magnSub1.*exp(i*phaseSub1);
 sub1_L=ifft(sub1_L);
 q=(abs(sub1_L).*cos(angle(sub1_L)));
 plot(q);
 axis([44000,45000,-1,+1]);
$\endgroup$
  • 2
    $\begingroup$ "But it looks like I can't get my desired output" -- it might help if you say what your desired output is, and what you got instead. $\endgroup$ – lxop Apr 28 '13 at 20:42
  • $\begingroup$ When I obtained the reconstructed signal in the simulink there were some change in amplitude! could you please tell me why? $\endgroup$ – user7542 Jan 13 '14 at 8:05
1
$\begingroup$

I'm not exactly sure what you mean by

I want to reconstruct the sampled signal

The term reconstruct has a special meaning in DSP and is related to converting a signal from discrete form to continuous using a DAC and a low-pass filter.

I'm going to guess that you are just looking to plot the signal in time domain, and if that is the case then your code would look like this:

[y, fs, nbits] = wavread('signal.wav')
sample_period = 1/fs;                % sample period is just the inverse of the sample rate
sample_time = size(y)/fs;            % total time represented by the samples 
time = 0:sample_period:sample_time;  % create the time array
plot(time,y)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.